advertisement:http://acm.hust.edu.cn/vjudge/problem/viewproblem.action?id=21426
The classic interval point of view, requires the number of points in each interval to meet the conditions K, the specific method is to put the right end point from small to large sort, and then every time check the number of points within the interval, if less, try to start from the right to add a little bit, so as to facilitate and other intervals to share a point
#include "iostream"
#include "algorithm"
#include "CString"
using namespace Std;
const int MAXN=1000+10;
typedef struct
{
int l,r;
}node;
BOOL CMP (node A1,node a2)
{
Return a1.r<a2.r;
}
Node A[MAXN];
int BOOK[MAXN*MAXN];
void Swap (node &aa)
{
int temp=aa.l;
AA.L=AA.R;
Aa.r=temp;
}
int main ()
{
int T;
cin>>t;
while (t--)
{
int k,n;
cin>>k>>n;
int max=-100000;
for (int i=0;i<n;i++)
{
cin>>a[i].l>>a[i].r;
if (A[I].L>A[I].R) swap (a[i]);
a[i].l+=10000;
a[i].r+=10000;
if (MAX<A[I].R) MAX=A[I].R;
}
Sort (a,a+n,cmp);
memset (book,0,sizeof (book));
int sum=0;
for (int i=0;i<n;i++)
{
int flag=0;
for (int j=a[i].l;j<=a[i].r;j++)//first check whether the interval has met the condition
{
if (Book[j]) flag++;
}
if (flag>=k) continue;
Else
{
for (int j=a[i].r;j>=a[i].l&&flag<k;j--)//try to put the dot on the right
{
if (!book[j])
{
Book[j]=1;
flag++;
sum++;
}
}
}
}
cout<<sum<<endl;
for (int l=0;l<=max;l++)
if (Book[l]) cout<<l-10000<<endl;
if (T) cout<<endl;
}
return 0;
}
Interval Point-of-sourcing