【原题】
The Department of Recreation has decided that it must be more profitable, and it wants to sell advertising space along a popular jogging path at a local park. They have built a number of billboards (special signs for advertisements) along the path and have decided to sell advertising space on these billboards. Billboards are situated evenly along the jogging path, and they are given consecutive integer numbers corresponding to their order along the path. At most one advertisement can be placed on each billboard.
A particular client wishes to purchase advertising space on these billboards but needs guarantees that every jogger will see it's advertisement at least K times while running along the path. However, different joggers run along different parts of the path.
Interviews with joggers revealed that each of them has chosen a section of the path which he/she likes to run along every day. Since advertisers care only about billboards seen by joggers, each jogger's personal path can be identified by the sequence of billboards viewed during a run. Taking into account that billboards are numbered consecutively, it is sufficient to record the first and the last billboard numbers seen by each jogger.
Unfortunately, interviews with joggers also showed that some joggers don't run far enough to see K billboards. Some of them are in such bad shape that they get to see only one billboard (here, the first and last billboard numbers for their path will be identical). Since out-of-shape joggers won't get to see K billboards, the client requires that they see an advertisement on every billboard along their section of the path. Although this is not as good as them seeing Kadvertisements, this is the best that can be done and it's enough to satisfy the client.
In order to reduce advertising costs, the client hires you to figure out how to minimize the number of billboards they need to pay for and, at the same time, satisfy stated requirements.
Input
The first line of the input consist of an integer indicating the number of test cases in theinput. Then there's a blank line and the test cases separated by a blank line.
The first line of each test case contains two integers K and N (1 ≤ K, N ≤ 1000) separated by a space. K is the minimal number of advertisements that every jogger must see, and N is the total number of joggers.
The following N lines describe the path of each jogger. Each line contains two integers Ai and Bi (both numbers are not greater than 10000 by absolute value). Ai represents the first billboard number seen by jogger number i and Bi gives the last billboard number seen by that jogger. During a run, jogger i will see billboards Ai, Bi and all billboards between them.
Output
On the first line of the output fof each test case, write a single integer M. This number gives the minimal number of advertisements that should be placed on billboards in order to fulfill the client's requirements. Then write M lines with one number on each line. These numbers give (in ascending order) the billboard numbers on which the client's advertisements should be placed. Print a blank line between test cases.
Sample input
1
5 10
1 10
20 27
0 -3
15 15
8 2
7 30
-1 -10
27 20
2 9
14 21
Sample output for the sample input
19
-5
-4
-3
-2
-1
0
4
5
6
7
8
15
18
19
20
21
25
26
27
这题目莫名的wawawa,一开始数组开错了,然后加的值搞错了,然后看着看着又感觉排序错了....终于ac了。
贪心的区间题目深入理解:
![](https://img-my.csdn.net/uploads/201208/15/1345038093_5414.jpg)
引用别人的图,首先我们可以很清楚的看到排序后,应该以右边开始插广告,从上到下,这样子的贪心一定是最优的,可以自行理解一下,另外,我们在代码中要注意细节,例如负数的处理,直接+10000变成正数,最后输出再-10000,然后有些区间题目是要要求当a.y==b.y,对a.x b.x也是要求排序的,这里是没有什么要求的,因此无需理会,好了就讲那么多。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct node
{
int x;
int y;
} a[20009];
int vis[20009];
int cmp(node a,node b)
{
return a.y<b.y;
}
/*
1
5 10
1 10
20 27
0 -3
15 15
8 2
7 30
-1 -10
27 20
2 9
14 21
*/
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,n,m,k,temp,j;
int count=0;
scanf("%d%d",&m,&n);
memset(vis,0,sizeof(vis));
int Min=100000;
for(i=0; i<=n-1; i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
if(a[i].x>a[i].y) swap(a[i].x,a[i].y);
a[i].x+=10000;
a[i].y+=10000;
Min=min(a[i].x,Min);
}
sort(a,a+n,cmp);
for(i=0; i<=n-1; i++)
{
k=a[i].y-a[i].x+1;//区间长度
if(k<=m)//不足,直接所有点都贴广告
{
for(j=a[i].y; j>=a[i].x; j--)
{
if(!vis[j])
{
count++;
vis[j]=1;
}
}
}
else
{
temp=0;//临时变量
for(j=a[i].y; j>=a[i].x; j--)
{
if(vis[j])
{
temp++; //在这个区间已经贴了这么多个
}
}
// if(temp>=m) continue;
for(j=a[i].y; j>=a[i].x&&temp<=m-1; j--)
{
if(!vis[j])
{
vis[j]=1;
temp++;
count++;
if(temp==m) break;
}
}
}
}
printf("%d\n",count);
for(i=0; i<=20005; i++)
{
if(vis[i])printf("%d\n",i-10000);
}
if(t) printf("\n");
}
}