UVa 10148 Advertisement: A single processing of greedy & labeling methods

Source: Internet
Author: User

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 is more profitable, and it wants to sell advertising spaces along a P Opular 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 ing spaces on these billboards. Billboards are situated evenly along the jogging path, and they are given the integer consecutive numbers to the IR order along the path. At most one advertisement can is placed on each billboard.

A particular client wishes to purchase advertising spaces on this billboards but needs guarantees that every jogger would s EE it ' s advertisement at LEASTK's while running along the path. However, different joggers run along different parts of the path.

Interviews with joggers revealed so 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 Sequenc E of billboards viewed during a run. Taking into account that billboards are numbered consecutively, it are sufficient to record the "the" and the last Billboar D numbers seen by each jogger.

Unfortunately, interviews with joggers also showed this some joggers don ' t run far enough to seek billboards. Some of them are in such bad shape that they get to the one billboard (here, the the "I" and "last Billboard numbers" for Their path would be identical). Since Out-of-shape Joggers won ' t get to seek billboards, the client requires that they. Advertisement on every Oard along their section of the path. Although this isn't as good as them Seeingk advertisements, this is the best so can and it enough to satisfy The client.

In order to reduce advertising costs, the client hires your to figure out how to minimize the number of billboards they nee D to pay for and, in the same time, satisfy stated requirements.
Input

The "a" of the input consist 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 "a" of each test case contains two integers K and N (1≤k, n≤1000) separated by a. K is the minimal number of advertisements to every jogger must, 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 to 10000 by absolute value). Ai represents the "billboard" number seen by jogger Numberi and Bi gives the last billboard number seen by that jogger . During a run, joggeri'll, Billboards Ai, Bi and all billboards between them.
Output

On the "the" output fof each test case, write a single integer M. This number gives the minimal number of advertisements, should is placed on billboards into order to fulfill the client ' s requirements. Then Writem lines with one. These numbers give (in ascending order) the Billboard numbers on which the client ' s advertisements should to 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
21st
25
26
27


Train of thought: the right end of the interval to sort, and then start from the right endpoint processing each interval, according to the simulation can be.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Complete code:

/*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;///row Right} p[1005];  
    BOOL Vis[n + N + 5];///here put the ad 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);///Note 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) {fo R (j = p[i].l J <= p[i].r; ++j) if (!vis[j)) ++cnt, vis[j] = true;///using labeling is more convenient than direct output (To handle multiple intervals is complex, it is simpler to study only one interval at a time} else {temp = k; for (j = p[i].l J <= p[i].r; ++j) if (vis[j))--temp;///statistics Billboard number, note that it may be counted as negative 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; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.