题目链接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3953

题意

给出N个区间,求去掉某些区间,使得剩下的区间中,任何的三个区间都不两两相交。

思路

将所有区间 以左端点为键值从小到大排序

然后三个三个一组 进行判断

如果 这三个中有两两相交的 那么就删去右端点最大的 因为这个区间对答案的贡献最小

然后三个区间当中没有两两相交的,那么下一次进来的区间就替换掉右端点最小的。

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 5e4 + 5;
const int MOD = 1e9 + 7; int ans[maxn]; int pos; struct Node
{
int id;
int l, r;
void read()
{
scanf("%d%d", &l, &r);
}
}q[maxn]; Node v[5]; bool comp(Node x, Node y)
{
return x.l < y.l;
} bool comp2(Node x, Node y)
{
return x.r < y.r;
} bool comp3(Node x, Node y)
{
return x.r > y.r;
} void solve()
{
sort(v, v + 3, comp);
bool flag = ((v[1].l <= v[0].r) && (v[2].l <= v[0].r) && (v[2].l <= v[1].r));
if (flag)
{
sort(v, v + 3, comp2);
ans[pos++] = v[2].id;
}
else
sort(v, v + 3, comp3);
} int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
q[i].read();
q[i].id = i;
}
sort(q + 1, q + 1 + n, comp);
v[0] = q[1];
v[1] = q[2];
pos = 0;
for (int i = 3; i <= n; i++)
{
v[2] = q[i];
solve();
}
sort(ans, ans + pos);
printf("%d\n", pos);
for (int i = 0; i < pos; i++)
{
if (i)
printf(" ");
printf("%d", ans[i]);
}
printf("\n");
}
}

最新文章

  1. jQuery easyui treegrid无法传参到后台bugger一记
  2. [转] Android OkHttp完全解析 是时候来了解OkHttp了
  3. Java GC CMS 日志分析
  4. ACM学习-POJ-1003-Hangover
  5. PHP Cookei记录用户历史浏览信息的代码
  6. JavaWeb之response响应中文乱码问题
  7. Linux发行版 CentOS6.5下删除分区操作
  8. 浅谈 ThreadLocal
  9. softmax 损失函数求导过程
  10. mysql 简称
  11. JAVA自学笔记08
  12. IO调度算法的理解(转载)
  13. Android如何在http头信息里设置参数
  14. 在PHP5.3以上版本运行ecshop出现的问题及解决方案
  15. Vue + Element UI 实现权限管理系统(搭建开发环境)
  16. sql点滴—mysql中查询表的信息
  17. Maximal Rectangle leetcode java
  18. VM虚拟机如何和主机共享文件夹或文件
  19. Connect C# to MySQL
  20. HTML子页面保存关闭并刷新父页面

热门文章

  1. LeetCode OJ--Rotate Image
  2. Oracle单个datafile大小的限制
  3. SourceTree免注册并连码云
  4. Django简单粗暴快速发送邮件!
  5. Codeforces Round #321 (Div. 2) Kefa and First Steps 模拟
  6. Oracle中PL/SQL 范例
  7. Maven的POM简单理解
  8. tensorflow搭建神经网络基本流程
  9. 【转载】面向切面编程(AOP)学习
  10. Git学习0基础篇(下)