本文已参与「新人创作礼」活动,一起开启掘金创作之路。
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 i -th segment is [li;ri] (li≤ri ) and it covers all integer points j 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 n and k (1≤k≤n≤200 ) — the number of segments and the maximum number of segments by which each integer point can be covered.
The next n lines contain segments. The i -th line contains two integers li and ri (1≤li≤ri≤200 ) — the endpoints of the i -th segment.
Output
In the first line print one integer m (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
题意:给定n个线段,线段可以相交,第i个线段覆盖的区间为[li,ri],问最少删除多少个线段让覆盖每个点的线段数量小于等于k。
思路:排好序,扫描~只是大的数据就过不去 T—T.....
Code:
#include<stdio.h>
#include<string.h>
#include<algorithm>
typedef long long ll;
const int N=1e5+10;
int book[210],ans[210];
using namespace std;
struct node
{
int x,y,c,m;
} q[210];
int cmp(node a,node b)
{
if(a.y==b.y)
return a.c<b.c;
return a.y<b.y;
}
int main()
{
int n,k;
while(~scanf("%d %d",&n,&k))
{
int a,b;
for(int i=0; i<n; i++)
{
scanf("%d %d",&a,&b);
q[i].x=a;
q[i].y=b;
q[i].c=b-a;
q[i].m=i;
}
sort(q,q+n,cmp);
int kk=0,g=0;
for(int i=0; i<n; i++)
{
int flag=0;
for(int j=q[i].x; j<=q[i].y; j++)
{
kk=book[j]+1;
if(kk>k)
{
ans[g++]=q[i].m+1;
flag=1;
break;
}
}
if(flag==0)
for(int j=q[i].x; j<=q[i].y; j++)
book[j]++;
}
printf("%d\n",g);
for(int i=0; i<g; i++)
printf("%d%c",ans[i],i==g-1?'\n':' ');
}
return 0;
}
太难了!~
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<set>
typedef long long ll;
const int N=2e5+10;
using namespace std;
struct node
{
int y;
int id;
};
bool operator<(node a,node b)
{
if(a.y!=b.y)return a.y<b.y;
return a.id<b.id;
}
vector<node>g[N];
vector<int>a;
node q;
int main()
{
int x,y,n,k;
scanf("%d %d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d %d",&x,&y);
q.y=y;q.id=i;
g[x].push_back(q);
}
set<node>s;
for(int i=1;i<N;i++)
{
while(s.size()&&(*s.begin()).y<i)
s.erase(*s.begin());
for(int j=0;j<g[i].size();j++)
s.insert(g[i][j]);
while(s.size()>k)
{
a.push_back((*s.rbegin()).id);
s.erase(*s.rbegin());
}
}
printf("%d\n",a.size());
int l=a.size();
for(int i=0;i<l;i++)
printf("%d%c",a[i],i==l-1?'\n':' ');
return 0;
}