区间选点问题,即数轴上有n个闭区间[l1i, ri],取尽量少的点,使得每个区间内都至少有一个点。

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

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个人,知道了他们每个人每天在街上走的路段。现在要求找到一些广告位,使得广告位数量最少,但是要求调查到的那 些每人至少看到广告K次。如果有人走的路段广告位少于K个,那么要求他在这个路段的所有广告位都要看到。输出要求的广告位的位置。

【分析】
假设每个区间是[li, ri], 那么先按照每个区间的ri从小到大排序,若ri相等,则按照li从大到小排序。
下图是排好序的四个区间。为了使得广告位数量最少,那么就要让这些广告位尽量的处在越多人重复经过的地方越好。
观察下图可以看出,为了让重叠部分越多,那么每个区间选点时,就要让这些点尽可能的往改区间的右边靠。
在对每个区间选点时,先查看该区间之前已经被选好几个点了,如果不够的话再补上。

代码(看过题解):

Note: solve函数中的for循环重点注意理解掌握!!!
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = ;
const int ADD = ;
struct Seg
{
int L, R;
bool operator < (const Seg &a) const
{
if(R != a.R) return R < a.R;
else return L > a.L;
}
}seg[maxn];
int vis[*ADD];
void solve(int n, int k)
{
memset(vis, , sizeof(vis));
int cnt = ;
for(int i = ; i < n; i++)
{
if(seg[i].R - seg[i].L + <= k)
{
for(int j = seg[i].L; j <= seg[i].R; j++)
{
if(!vis[j])
cnt++, vis[j] = ;
}
}
else
{
int num = ;
for(int j = seg[i].L; j <= seg[i].R; j++)
{
if(vis[j])
num++;
}
if(num >= k) continue;
for(int j = seg[i].R; j >= seg[i].L; j--)
{
if(!vis[j]) {
cnt++;
num++;
vis[j] = ;
if(num >= k) break;
}
}
}
}
printf("%d\n", cnt);
for(int i = ; i < *ADD; i++)
{
if(vis[i])
printf("%d\n", i-ADD);
}
}
int main()
{
int T;
scanf("%d", &T);
for(int kase = ; kase < T; kase++)
{
if(kase) printf("\n");
int k, n;
scanf("%d%d", &k, &n);
for(int i = ; i < n; i++)
{
scanf("%d%d", &seg[i].L, &seg[i].R);
seg[i].L += ADD; seg[i].R += ADD;
if(seg[i].L > seg[i].R) {int t = seg[i].L; seg[i].L = seg[i].R; seg[i].R = t;}
}
sort(seg, seg+n);
solve(n, k);
}
return ;
}
 

【区间选点问题】uva 10148 - Advertisement的更多相关文章

  1. UVa 10148 - Advertisement

    题目大意:有一些广告牌,为了使跑步者看到固定数量的广告,设计所需租用的最少数量的广告牌. 其实就是区间选点问题:数轴上有n个区间[ai, bi],取尽量少的点,使得每一个区间都至少有一个点.首先对区间 ...

  2. UVa 1615 Highway (贪心,区间选点问题)

    题意:给定一个数 n 个点,和一个d,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里德距离不超过d. 析:首先这是一个贪心的题目,并且是区间选点问题,什么是区间选点呢, ...

  3. UVA 1615 Highway 高速公路 (区间选点)

    题意:在一条线段上选出尽量少的点,使得和所有给出的n个点距离不超过D. 分别计算出每个点在线段的满足条件的区间,然后就转化成了区间选点的问题了,按照右端点排序,相同时按照左端点排序,按照之前的排序一定 ...

  4. 紫书 习题8-11 UVa 1615 (区间选点问题)

    这个点就是贪心策略中的区间选点问题. 把右端点从大到小排序, 左端点从小到大排序. 每次取区间右端点就可以了, 到不能覆盖的时候就ans++, 重新取点 ps:这道题不考虑精度也可以过 要着重复习一下 ...

  5. UVALive 2519 Radar Installation 雷达扫描 区间选点问题

    题意:在坐标轴中给出n个岛屿的坐标,以及雷达的扫描距离,要求在y=0线上放尽量少的雷达能够覆盖全部岛屿. 很明显的区间选点问题. 代码: /* * Author: illuz <iilluzen ...

  6. UVAlive 2519 Radar Installation (区间选点问题)

    Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...

  7. hdu 4883 区间选点

    昨天比赛的时候没有做出来,本来是想用贪心的,可是贪了好久都没有招, 今天在网上搜了解题报告~好像说这是一类区间选点问题: 有一个好的做法: (1)首先把题目中的时间全转化为分钟,那么区间就在0-144 ...

  8. 贪心算法----区间选点问题(POJ1201)

    题目: 题目的大致意思是,给定n个闭区间,并且这个闭区间上的点都是整数,现在要求你使用最少的点来覆盖这些区间并且每个区间的覆盖的点的数量满足输入的要求点覆盖区间的数量. 输入: 第一行输入n,代表n个 ...

  9. UVA-1615 Highway (贪心,区间选点)

    题目大意:有一条沿x轴正方向,长为L的高速公路,n个村庄,要求修建最少的公路出口数目,使得每个村庄到出口的距离不大于D. 题目分析:区间选点问题.在x轴上,到每个村庄距离为D的点有两个(超出范围除外) ...

随机推荐

  1. 网页加载速度优化2--先加载css,然后再加载js文件。

    网页加载时,是按从上到下,从左到右的顺序加载的.所以一定要先加载css文件(不要让用户看到一个杂乱无章的页面),最后再加载js文件,js一般都是处理功能的,所以不需要提前加载.先给用户观感,再给用户上 ...

  2. python join和split和strip用法

    python join 和 split方法的使用,join用来连接字符串,split恰好相反,拆分字符串的. strip()为去除开头结尾指定的字符,空着时是去除空白字符\t,\n,\r意思 1.jo ...

  3. InetAddress

    InetAddress对域名进行解析是使用本地机器配置或者网络命名服务(如域名系统(Domain Name System,DNS)和网络信息服务(Network Information Service ...

  4. PopupWindow 问题集锦

    1.响应返回键/响应键盘事件(onKeyListener) 最近在做PopupWindow, 发现使用PopupWindow一出现,不会响应popup外面的事件,经过资料查找,发现有两种方法可以响应外 ...

  5. STM32硬件调试详解

    STM32的基本系统主要涉及下面几个部分: 一.电源 1).无论是否使用模拟部分和AD部分,MCU外围出去VCC和GND,VDDA.VSSA.Vref(如果封装有该引脚)都必需要连接,不可悬空: 2) ...

  6. [OC Foundation框架 - 7] NSArray的创建与遍历

    NSArray是不可变的,不能先创建再添加元素 NSArray可以放入任何OC对象,但不能放入基本数据类型.结构体.枚举等非OC对象 不能存储nil   A.常用方法1 创建 返回用量 是否含有某元素 ...

  7. VirtualBox NAT方式与主机互相通信

    之前说过,桥接方式适合在统一的网络环境中使用(一样的网关和许可). 如果网络环境发生改变,那就难堪了 -- 这就是我遇到的问题,公司里每人的IP都是固定的. 解决办法,改为NAT网络地址转换模式. 但 ...

  8. JAVA的设计模式之单例设计模式

    1.确保一个类只有一个实例,自行提供这个实例并向整个系统提供这个实例. 1)理论 Java Singleton模式主要作用是保证在Java应用程序中,一个类Class只有一个实例存在. 使用Singl ...

  9. Oracle-12541:TNS:无监听程序 .

    背景:自己机子做oracle服务器,其他机子可以ping得通我的机子,但是jdbc就是连不上,后来用plsql连出现无监听程序.... 我昨天重新安装Oracle后,用PL/SQL Developer ...

  10. IDF实验室-简单的ELF逆向 writeup

    题目:http://ctf.idf.cn/index.php?g=game&m=article&a=index&id=39 下载得到ElfCrackMe1文件,直接用IDA打开 ...