10148 - Advertisement
Time limit: 3.000 seconds
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1089
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 leastK 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 seeK 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 seeK 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 seeingK 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 ≤ K, N ≤ 1000) separated by a space.K is the minimal number of advertisements that every jogger must see, andN is the total number of joggers.
The following N lines describe the path of each jogger. Each line contains two integersAi and Bi (both numbers are not greater than 10000 by absolute value).Ai represents the first billboard number seen by jogger numberi and Bi gives the last billboard number seen by that jogger. During a run, joggeri 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 writeM 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
思路:對區間右端點排序,然後從右端點開始處理每個區間,按題意類比即可。
查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
完整代碼:
/*0.086s*/ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 10000; struct T { int l, r; bool operator < (const T a) const { return r < a.r;///排右邊 } } p[1005]; bool vis[N + N + 5];///這裡放廣告 int main() { int cas, i, j, k, n, cnt, a, b, temp; scanf("%d", &cas); while (cas--) { scanf("%d%d", &k, &n); for (i = 0; i < n; ++i) { scanf("%d%d", &a, &b); if (a > b) swap(a, b);///注意 p[i].l = N + a, p[i].r = N + b; } sort(p, p + n); memset(vis, 0, sizeof(vis)); cnt = 0; for (i = 0; i < n; ++i) { if (p[i].r - p[i].l + 1 <= k) { for (j = p[i].l; j <= p[i].r; ++j) if (!vis[j]) ++cnt, vis[j] = true;///採用標記法要比直接輸出更方便(因為處理多個區間很複雜,一次只研究一個區間更簡單) } else { temp = k; for (j = p[i].l; j <= p[i].r; ++j) if (vis[j]) --temp;///統計廣告牌數,注意可能統計成負值 for (j = p[i].r; temp > 0; --j) if (!vis[j]) ++cnt, vis[j] = true, --temp; } } printf("%d\n", cnt); for (i = 0; i < N + N + 5; ++i) if (vis[i]) printf("%d\n", i - N); if (cas) putchar(10); } return 0; }