ZOJ - 3953Intervals(贪心)
2017-04-11 15:54
295 查看
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.
给你n个区间的左右端点,让你取走一些区间,使得任何三个区间都不两两相交。
思路:
贪心,首先将所有区间以左端点从小到大排序,然后每次判断当前的三个区间是否两两相交,如果两两相交则删除右端点值最大的那个区间,因为这样对后面的影响会最小。如果不相交则继续添加区间,每三个一判断。#include<bits/stdc++.h>
using namespace std;
const int maxn = 50005;
struct Intervals
{
int l, r, id;
}a[maxn], tmp[5];
bool cmp1(Intervals p, Intervals q)
{
if(p.l == q.l)
return p.r < q.r;
return p.l < q.l;
}
bool cmp2(Intervals p, Intervals q)
{
return p.r > q.r;
}
bool isInterval(Intervals x, Intervals y, Intervals z)
{
return y.l <= x.r && z.l <= y.r && z.l <= x.r;
}
int ans[maxn], cnt;
int main()
{
int n, T;
scanf("%d", &T);
while(T--)
{
cnt = 0;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &a[i].l, &a[i].r);
a[i].id = i;
}
sort(a+1, a+n+1, cmp1);
tmp[1] = a[1];
tmp[2] = a[2];
for(int i = 3; i <= n; i++)
{
tmp[3] = a[i];
sort(tmp+1, tmp+4, cmp1);
if(isInterval(tmp[1], tmp[2], tmp[3]))
{
sort(tmp+1, tmp+4, cmp2);
ans[cnt++] = tmp[1].id;
swap(tmp[1], tmp[3]);
}
else
sort(tmp+1, tmp+4, cmp2);
}
sort(ans, ans+cnt);
printf("%d\n", cnt);
for(int i = 0; i < cnt; i++)
printf("%d%c", ans[i], i == cnt-1 ? '\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
43 5 7 10题目大意:
给你n个区间的左右端点,让你取走一些区间,使得任何三个区间都不两两相交。
思路:
贪心,首先将所有区间以左端点从小到大排序,然后每次判断当前的三个区间是否两两相交,如果两两相交则删除右端点值最大的那个区间,因为这样对后面的影响会最小。如果不相交则继续添加区间,每三个一判断。#include<bits/stdc++.h>
using namespace std;
const int maxn = 50005;
struct Intervals
{
int l, r, id;
}a[maxn], tmp[5];
bool cmp1(Intervals p, Intervals q)
{
if(p.l == q.l)
return p.r < q.r;
return p.l < q.l;
}
bool cmp2(Intervals p, Intervals q)
{
return p.r > q.r;
}
bool isInterval(Intervals x, Intervals y, Intervals z)
{
return y.l <= x.r && z.l <= y.r && z.l <= x.r;
}
int ans[maxn], cnt;
int main()
{
int n, T;
scanf("%d", &T);
while(T--)
{
cnt = 0;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &a[i].l, &a[i].r);
a[i].id = i;
}
sort(a+1, a+n+1, cmp1);
tmp[1] = a[1];
tmp[2] = a[2];
for(int i = 3; i <= n; i++)
{
tmp[3] = a[i];
sort(tmp+1, tmp+4, cmp1);
if(isInterval(tmp[1], tmp[2], tmp[3]))
{
sort(tmp+1, tmp+4, cmp2);
ans[cnt++] = tmp[1].id;
swap(tmp[1], tmp[3]);
}
else
sort(tmp+1, tmp+4, cmp2);
}
sort(ans, ans+cnt);
printf("%d\n", cnt);
for(int i = 0; i < cnt; i++)
printf("%d%c", ans[i], i == cnt-1 ? '\n' : ' ');
}
return 0;
}
相关文章推荐
- ZOJ - 3953Intervals 【贪心】
- ZOJ 3820 Building Fire Stations 贪心+树的直径
- ZOJ 3829 Known Notation --贪心+找规律
- 贪心+模拟 ZOJ 3829 Known Notation
- ZOJ 3778 Talented Chef (贪心+二分)
- ZOJ 3715 Kindergarten Election(枚举+贪心)
- ZOJ3953 Intervals 【贪心 双线程活动分配问题】
- ZOJ 3953 Intervals (贪心)
- ZOJ 2315 New Year Bonus Grant(贪心)
- ZOJ 3987 && 2017CCPC秦皇岛 G:Numbers(高精度+贪心)
- zoj 2109 || hdoj1009 FatMouse' Trade(贪心)
- ZOJ-3963-Heap Partition(贪心)(STL)
- ZOJ 3715 - Kindergarten Election 枚举后贪心...
- ZOJ Problem Set - 3829Known Notation(贪心)
- zoj 3627 Treasure Hunt II (贪心)
- zoj 3327 Friend Number 贪心
- ZOJ3829-贪心|模拟-Known Notation
- 贪心,二叉树搜索,ZOJ(2315)
- ZOJ 3829 Known Notation 贪心
- ZOJ 3946 Highway Project【dijkstra】【贪心】