Uva--10148(贪心)

2014-07-27 14:50:14

Problem F
"Advertisement"

 

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 K advertisements, 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 ≤ KN ≤ 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 AiBi 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

思路:把读入的数据当做一个区间(要使左<=右),根据区间右值从小到大排序,右值一样大按左值从大到小排序,然后点尽量放在右边(关于标记广告牌是否放的话直接hash QAQ)。(根据小白书P153)
 1 /*************************************************************************
 2     > File Name: r.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com
 5     > Created Time: Sun 27 Jul 2014 12:48:09 AM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 const int INF = 1000000000;
16 
17 int Case,k,n,used[20005],acnt;
18 
19 struct Seg{
20     int l,r;
21 }s[1005];
22 
23 bool cmp(Seg a,Seg b){
24     if(a.r == b.r)
25         return a.l > b.l;
26     return a.r < b.r;
27 }
28 
29 int Count(int x,int y){
30     int cnt = 0;
31     for(int i = x; i <= y; ++i)
32         if(used[i]) ++cnt;
33     return cnt;
34 }
35 
36 void Add(int ed,int cnt){
37     acnt += cnt;
38     for(int i = ed; cnt ; --i)
39         if(!used[i]){
40             used[i] = 1;
41             --cnt;
42         }
43 }
44 
45 int main(){
46     scanf("%d",&Case);
47     while(Case--){
48         scanf("%d%d",&k,&n);
49         memset(used,0,sizeof(used));
50         acnt = 0;
51         int al,ar,tem;
52         for(int i = 0; i < n; ++i){
53             scanf("%d%d",&al,&ar);
54             if(al > ar){
55                 tem = al;
56                 al = ar;
57                 ar = tem;
58             }
59             s[i].l = al + 10000;
60             s[i].r = ar + 10000;
61         }
62         sort(s,s + n,cmp);
63         for(int i = 0; i < n; ++i){
64             int tk = min(k,s[i].r - s[i].l + 1);
65             int num = tk - Count(s[i].l,s[i].r);
66             if(num > 0)
67                 Add(s[i].r,num);
68         }
69         printf("%d\n",acnt);
70         for(int i = 0; i <= 20000; ++i)
71             if(used[i]) printf("%d\n",i - 10000);
72         if(Case) printf("\n");
73     }
74     return 0;
75 }

 

 
posted @ 2014-07-27 14:53  Naturain  阅读(135)  评论(0编辑  收藏  举报