ZOJ - 3953 Intervals(贪心)
2017-04-15 22:58
316 查看
Intervals
Time Limit: 1 Second Memory Limit: 65536 KB Special Judge
Chiaki has n intervals and the i-th of them is [li, ri].
She wants to delete some intervals so that there does not exist three intervals a, b and c such that a intersects with b, b intersects with c and c intersects with a.
Chiaki is interested in the minimum number of intervals which need to be deleted.
Note that interval a intersects with interval b if there exists a real number x such that la ≤ x ≤ ra and lb ≤ x ≤ rb.
The first line contains an integer n (1 ≤ n ≤ 50000) -- the number of intervals.
Each of the following n lines contains two integers li and ri (1 ≤ li < ri ≤
109) denoting the i-th interval. Note that for every 1 ≤ i < j ≤ n, li ≠ lj or ri ≠ rj.
It is guaranteed that the sum of all n does not exceed 500000.
of the intervals to be deleted. If m equals to 0, you should output an empty line in the second line.
Author: LIN, Xi
Source: The 17th Zhejiang University Programming Contest Sponsored by TuSimple
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 50005;
struct node
{
int s,e,id;
bool operator < (const node & p)const
{
if(s == p.s)
return e < p.e;
else
return s < p.s;
}
}p
,tmp[4];
bool judge(node *a)
{
node x = a[0];
node y = a[1];
node z = a[2];
return x.e >= y.s && y.e >= z.s && x.e >= z.s;
}
bool cmp(node a,node b)
{
return a.e > b.e;
}
int ind
;
int main()
{
int t,n;
cin>>t;
while(t--)
{
scanf("%d",&n);
for(int i = 0;i < n;i++)
{
scanf("%d%d",&p[i].s,&p[i].e);
p[i].id = i+1;
}
int num = 0;
sort(p,p+n);
tmp[0] = p[0];
tmp[1] = p[1];
for(int i = 2;i < n;i++)
{
tmp[2] = p[i];
sort(tmp,tmp+3);
bool flag = false;
if(judge(tmp))
flag = true;
sort(tmp,tmp+3,cmp);
if(flag)
{
ind[num++] = tmp[0].id;
swap(tmp[0],tmp[2]);
}
}
sort(ind,ind+num);
printf("%d\n",num);
printf("%d",ind[0]);
for(int i = 1;i < num;i++)
printf(" %d",ind[i]);
printf("\n");
}
return 0;
}
Time Limit: 1 Second Memory Limit: 65536 KB Special Judge
Chiaki has n intervals and the i-th of them is [li, ri].
She wants to delete some intervals so that there does not exist three intervals a, b and c such that a intersects with b, b intersects with c and c intersects with a.
Chiaki is interested in the minimum number of intervals which need to be deleted.
Note that interval a intersects with interval b if there exists a real number x such that la ≤ x ≤ ra and lb ≤ x ≤ rb.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:The first line contains an integer n (1 ≤ n ≤ 50000) -- the number of intervals.
Each of the following n lines contains two integers li and ri (1 ≤ li < ri ≤
109) denoting the i-th interval. Note that for every 1 ≤ i < j ≤ n, li ≠ lj or ri ≠ rj.
It is guaranteed that the sum of all n does not exceed 500000.
Output
For each test case, output an integer m denoting the minimum number of deletions. Then in the next line, output m integers in increasing order denoting the indexof the intervals to be deleted. If m equals to 0, you should output an empty line in the second line.
Sample Input
1 11 2 5 4 7 3 9 6 11 1 12 10 15 8 17 13 18 16 20 14 21 19 22
Sample Output
4 3 5 7 10
Author: LIN, Xi
Source: The 17th Zhejiang University Programming Contest Sponsored by TuSimple
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 50005;
struct node
{
int s,e,id;
bool operator < (const node & p)const
{
if(s == p.s)
return e < p.e;
else
return s < p.s;
}
}p
,tmp[4];
bool judge(node *a)
{
node x = a[0];
node y = a[1];
node z = a[2];
return x.e >= y.s && y.e >= z.s && x.e >= z.s;
}
bool cmp(node a,node b)
{
return a.e > b.e;
}
int ind
;
int main()
{
int t,n;
cin>>t;
while(t--)
{
scanf("%d",&n);
for(int i = 0;i < n;i++)
{
scanf("%d%d",&p[i].s,&p[i].e);
p[i].id = i+1;
}
int num = 0;
sort(p,p+n);
tmp[0] = p[0];
tmp[1] = p[1];
for(int i = 2;i < n;i++)
{
tmp[2] = p[i];
sort(tmp,tmp+3);
bool flag = false;
if(judge(tmp))
flag = true;
sort(tmp,tmp+3,cmp);
if(flag)
{
ind[num++] = tmp[0].id;
swap(tmp[0],tmp[2]);
}
}
sort(ind,ind+num);
printf("%d\n",num);
printf("%d",ind[0]);
for(int i = 1;i < num;i++)
printf(" %d",ind[i]);
printf("\n");
}
return 0;
}
相关文章推荐
- ZOJ 3953 Intervals(区间贪心)
- ZOJ-3953-Intervals 贪心
- 浙大17年校赛(ZOJ 3953) Intervals[贪心]
- ZOJ 3953 Intervals (贪心/区间交)
- ZOJ-3953-Intervals【贪心】【17th浙大校赛】
- ZOJ3953 Intervals 【贪心 双线程活动分配问题】
- ZOJ 3953 Intervals (贪心)
- zoj3953 贪心 training 3
- ZOJ 3953 Intervals
- ZOJ 3953-贪心
- ZOJ-3953 Intervals,t
- zoj3953 Intervals 最大不重叠区间加强版 zoj排名第一~
- ZOJ3953 ZJU2017校赛(贪心)
- zoj3953 Intervals 最大不重叠区间加强版 zoj排名第一~
- ZOJ 3953 贪心+优先队列
- ZOJ 3953 Intervals
- ZOJ 3953 Intervals
- ZOJ 3829 Known Notation / 2014牡丹江区域赛 K (模拟, 贪心)
- zoj 1508 Intervals 差分约束系统
- ZOJ 1025 Wooden Sticks(贪心 基础题)