【链接】

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 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 ≤ 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

15 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个人,知道了他们每个人每天在街上走的路段。现在要求找到一些广告位,使得广告位数量最少,但是要求调查到的那些每人至少看到广告K次。如果有人走的路段广告位少于K个,那么要求他在这个路段的所有广告位都要看到。输出要求的广告位的位置。
【分析与总结】
经典贪心,区间选点问题。《算法入门经典》P154。
假设每个区间是【ai, bi】, 那么先按照每个区间的bi从小到大排序。
下图是排好序的四个区间。为了使得广告位数量最少,那么就要让这些广告位尽量的处在越多人重复经过的地方越好。
观察下图可以看出,为了让重叠部分越多,那么每个区间选点时,就要让这些点尽可能的往改区间的右边靠。
在对每个区间选点时,先查看该区间之前已经被选好几个点了,如果不够的话再补上。
【代码】
/** UVa: 10148 - Advertisement* Time: 0.080s* Author: D_Double**/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 20005
#define ADD 10000
using namespace std;
int K,N;
bool vis[MAXN];struct Node{int left;int right;friend bool operator < (const Node &a,const Node &b){return a.right<b.right;}
}arr[1005];void solve(){sort(arr,arr+N);memset(vis, 0, sizeof(vis));int sum=0; for(int i=0; i<N; ++i){if(arr[i].right-arr[i].left+1<=K){for(int j=arr[i].left; j<=arr[i].right; ++j)if(!vis[j]){++sum;vis[j]=true;}}else{int cnt=0;for(int j=arr[i].left; j<=arr[i].right; ++j)if(vis[j]){++cnt;}if(cnt>=K) continue;for(int j=arr[i].right; j>=arr[i].left; --j)if(!vis[j]){vis[j]=true;++cnt;++sum;if(cnt>=K)break;}}}printf("%d\n",sum);for(int i=0; i<20005; ++i)if(vis[i]){printf("%d\n",i-ADD);}
}int main(){int T;scanf("%d",&T);while(T--){scanf("%d%d",&K,&N);for(int i=0; i<N; ++i){scanf("%d%d",&arr[i].left,&arr[i].right);    arr[i].left+=ADD, arr[i].right+=ADD;if(arr[i].right<arr[i].left){int t=arr[i].left;arr[i].left=arr[i].right;arr[i].right=t;}}solve();if(T) printf("\n");}return 0;
}
——  生命的意义,在于赋予它意义。
          
     原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)

UVa 10148 - Advertisement相关推荐

  1. UVa 10148 - Advertisement

    题意: 在一个街道上有很多广告位,现在一家公司需要投放广告,要针对具体情况投放.给定一个K值,假如慢跑者经历K以上个路段,至少要看到K个广告位,经历少于等于K个,则需要让慢跑者看到全部广告位,求投放的 ...

  2. 提取了下刘汝佳推荐的题号...

    今天闲来没事上uva oj提取了下刘汝佳推荐的acm题号,原始数据如下: Volume 0. Getting Started    10055 - Hashmat the Brave Warrior ...

  3. UVA 11983 Weird Advertisement

    UVA_11983 这个题目可以转化成求被覆盖K次的矩形面积的并,只要将x2和y2都+1即可,相当于我们把每个单位正方形看成一个点. #include<stdio.h> #include& ...

  4. [搜索]UVa 129 困难的串

    题意:将一个包含两个相邻的重复子串的子串,称为"容易的串",其他为"困难的串". 输入正整数n和l,输出由前l个字符组成的,字典序第n小的困难的串. 输入样例: ...

  5. uva 401.Palindromes

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  6. Uva 3767 Dynamic len(set(a[L:R])) 树套树

    Dynamic len(set(a[L:R])) Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/in ...

  7. UVA 11752 超级幂

    UVA 11752 超级幂 Z - The Super Powers Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & ...

  8. UVa 11174 - Stand in a Line

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. UVa 10112 - Myacm Triangles

    UVa第一卷最后一题. 求内部不含点并且面积最大的三角形. 暴力. 代码如下: 1 #include<iostream> 2 #include<cstdio> 3 #inclu ...

最新文章

  1. Python之多线程
  2. python操作文件夹-Python文件操作大全,随机删除文件夹内的任意文件
  3. 用Mysql创建设备管理信息系统数据库(图解)
  4. OI模板のpoke流[大型考试复习必备/kl]
  5. 五年级数学上册用计算机探索规律,人教版小学五年级数学上册《用计算器探索规律》课后反思...
  6. Word文档显示标题3
  7. mybatis查询返回null解决方案
  8. 如何获取foreach循环当前迭代的索引?
  9. struts2进阶篇(3)
  10. baacloud苹果_Baacloud手机客户端下载
  11. H5营销互动小游戏源码
  12. 史上最完整的APP商城源码(含服务器)
  13. TDR/TDT测量原理分析
  14. OO包设计原则遵循度自动分析检查工具JDM简介(原创)
  15. 浅谈c++中upper_bound与lower_bound的用法
  16. 【matlab】常微分方程的数值解法
  17. UAP开发中遇到的问题
  18. 输入姓名并分别输出姓和名
  19. Echarts实现自定义图标——风向图
  20. 华为AP3010DN-V2 Fit转Fat

热门文章

  1. matlab无限长一维原子链,固体物理 03-03一维双原子链
  2. 自定义控件解决android中TextView中英文换行问题
  3. 让你的工作更高效!快来看看如何使用内网穿透
  4. 室内场馆360全景拍摄的注意事项
  5. Tcl/Tk入门(上)
  6. 什么是大小端?如何判断大小端
  7. 【AMD和CMD的区别】
  8. 成都拓嘉辰丰:拼多多子账号建立的方法?
  9. 我改变世界、我已看透、我不再是个程序员-IT创世诸神如是说
  10. HTML——前端实时可视化开发工具