zoukankan      html  css  js  c++  java
  • UVA 10148 Advertisement (贪心 + 区间选点问题)

    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

    题意: 

            一家公司要张贴广告,要保证每个慢跑者至少看到n个广告,现在一共有m个慢跑者,每个慢跑者跑的路径都是不一样的,要求出最少要张贴的广告数,以及张贴的位置,要按从小到大输出。

    思路:

            贪心、区间选点的问题,慢跑者跑的路径是一个区间,把这些区间[l,r]按r从小到大排序,如果r相同的,就按l从大到小排序。然后每次选点就从r开始从右往左添加点,如果大于等于n个点,说明这个区间已经满足条件了。所有区间都进行一次这样的操作,操作过程中把添加点的位置保存下来,最后排序输出即可。

    代码:

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    
    int t;
    int n, m, vis[20005];
    int out[20005], outnum;
    struct People {
        int start;
        int end;
        int dnum;
    } p[1005];
    
    int cmp(People a, People b) {
        if (a.end != b.end)
    	return a.end < b.end;
        return a.start > b.start;
    }
    int main() {
        scanf("%d", &t);
        while (t --) {
    	outnum = 0;
    	memset(p, 0, sizeof(p));
    	memset(vis, 0, sizeof(vis));
    	scanf("%d%d", &n, &m);
    	for (int i = 0; i < m; i ++) {
    	    scanf("%d%d", &p[i].start, &p[i].end);
    	    if (p[i].start > p[i].end)
    		swap(p[i].start, p[i].end);
    	}
    	sort(p, p + m, cmp);
    	for (int i = 0; i < m; i ++) {
    	    for (int j = p[i].end; j >= p[i].start; j --) {
    		if (vis[j + 10000]) 
    		    p[i].dnum ++;
    		if (p[i].dnum == n)
    		    break;
    	    }
    	    for (int j = p[i].end; j >= p[i].start; j --) {
    		if (p[i].dnum == n)
    		    break;
    		if (!vis[j + 10000]) {
    		    p[i].dnum ++;
    		    out[outnum ++] = j;
    		    vis[j + 10000] = 1;
    		}
    	    }
    	}
    	sort(out, out + outnum);
    	printf("%d
    ", outnum);
    	for (int i = 0; i < outnum; i ++)
    	    printf("%d
    ", out[i]);
    	if (t) printf("
    ");
        }
        return 0;
    }


  • 相关阅读:
    MySQL · 引擎特性 · InnoDB 事务锁简介
    锁大全与 GDB调试
    docker(4):coreos+docker+rancher真厉害
    TIDB ---NEW SQL
    Linux的内存回收和交换
    TLS握手优化详解
    北风网JAVA 大数据培训
    MySQL 5.7 深度解析: JSON数据类型使用
    MySQL 5.7 深度解析: 临时表空间
    在Linux最大打开文件数限制下 MySQL 对参数的调整
  • 原文地址:https://www.cnblogs.com/bbsno1/p/3271514.html
Copyright © 2011-2022 走看看