Too Many Segments (easy version) CodeForces - 1249D1(贪心+差分)
題意
給多組線段,而每一個(gè)點(diǎn)的覆蓋次數(shù)不超過K,每次可去除一個(gè)線段,問最少去多少線段以及線段的位置。
The only difference between easy and hard versions is constraints.
You are given nn segments on the coordinate axis OX. Segments can intersect, lie inside each other and even coincide. The ii-th segment is [li;ri] (li≤ri) and it covers all integer points jj such that li≤j≤ri.
The integer point is called bad if it is covered by strictly more than k segments.
Your task is to remove the minimum number of segments so that there are no bad points at all.
Input
The first line of the input contains two integers nn and kk (1≤k≤n≤200) — the number of segments and the maximum number of segments by which each integer point can be covered.
The next nn lines contain segments. The ii-th line contains two integers lili and riri (1≤li≤ri≤200) — the endpoints of the ii-th segment.
Output
In the first line print one integer mm (0≤m≤n) — the minimum number of segments you need to remove so that there are no bad points.
In the second line print mm distinct integers p1,p2,…,pm(1≤pi≤n) — indices of segments you remove in any order. If there are multiple answers, you can print any of them.
Examples
Input
7 2
11 11
9 11
7 8
8 9
7 8
9 11
7 9
Output
3
1 4 7
Input
5 1
29 30
30 30
29 29
28 30
30 30
Output
3
1 2 4
Input
6 1
2 3
3 3
2 3
2 2
2 3
2 3
Output
4
1 3 5 6
官方題解:
In this problem, the following greedy solution works: let’s find the leftmost point covered by more than kk segments. We should fix it somehow. How to do it? Let’s find some segment that was not removed already, it covers this point and its rightmost end is maximum possible, and remove this segment.
You can implement it in any time you want, even in O(n3) naively.
百度翻譯:
在這個(gè)問題中,下面的貪婪解決方案是有效的:讓我們找到被超過k段覆蓋的最左邊的點(diǎn)我們應(yīng)該設(shè)法解決它。怎么做?讓我們找到一些尚未被刪除的段,它覆蓋了這一點(diǎn),它的最右邊是最大可能的,并刪除這個(gè)段。
你可以在任何時(shí)候?qū)崿F(xiàn)它,即使是在O(n3)中。
思路
用差分?jǐn)?shù)組找到每個(gè)點(diǎn)的覆蓋次數(shù),若超過k次則去掉幾個(gè)右邊較大的(因?yàn)槭菑淖蟮接?#xff0c;所以不用考慮左邊)
AC代碼
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=300; int n,k,d[N],num; struct node {int u,v; } s[N]; int main() {while(~scanf("%d%d",&n,&k)){memset(d,0,sizeof(d));int mx=-1;for(int i=1; i<=n; i++)/*差分?jǐn)?shù)組*/{scanf("%d %d",&s[i].u,&s[i].v);mx=max(mx,s[i].u);mx=max(mx,s[i].v);d[s[i].u]++;d[s[i].v+1]--;}for(int i=1; i<=mx; i++)d[i]+=d[i-1]; //差分求每個(gè)點(diǎn)被覆蓋值int book[N];memset(book,0,sizeof(book));int flag=0;num=0;for(int j=1; j<=mx; j++){while(d[j]>k){flag=1;int tab=0;for(int i=1; i<=n; i++)/*從n條邊中找到i所處的線段,并在所有的線段中找到最右邊最大的那個(gè)線段*/{if(book[i]==0&&s[i].u<=j&&j<=s[i].v&&(tab==0||s[i].v>s[tab].v))tab=i;}if(tab==0)break;for(int i=s[tab].u; i<=s[tab].v; i++)d[i]--;book[tab]=1;num++;}}if(flag==0)printf("0\n");else{printf("%d\n",num);for(int i=1; i<=n; i++)if(book[i])printf("%d ",i);printf("\n");}}return 0; }總結(jié)
以上是生活随笔為你收集整理的Too Many Segments (easy version) CodeForces - 1249D1(贪心+差分)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Power Strings POJ -
- 下一篇: 舒脑欣滴丸功效与作用