【区间选点问题】uva 10148 - Advertisement
区间选点问题,即数轴上有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 ≤ K, N ≤ 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 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 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从大到小排序。
下图是排好序的四个区间。为了使得广告位数量最少,那么就要让这些广告位尽量的处在越多人重复经过的地方越好。
观察下图可以看出,为了让重叠部分越多,那么每个区间选点时,就要让这些点尽可能的往改区间的右边靠。
在对每个区间选点时,先查看该区间之前已经被选好几个点了,如果不够的话再补上。
#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的更多相关文章
- UVa 10148 - Advertisement
题目大意:有一些广告牌,为了使跑步者看到固定数量的广告,设计所需租用的最少数量的广告牌. 其实就是区间选点问题:数轴上有n个区间[ai, bi],取尽量少的点,使得每一个区间都至少有一个点.首先对区间 ...
- UVa 1615 Highway (贪心,区间选点问题)
题意:给定一个数 n 个点,和一个d,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里德距离不超过d. 析:首先这是一个贪心的题目,并且是区间选点问题,什么是区间选点呢, ...
- UVA 1615 Highway 高速公路 (区间选点)
题意:在一条线段上选出尽量少的点,使得和所有给出的n个点距离不超过D. 分别计算出每个点在线段的满足条件的区间,然后就转化成了区间选点的问题了,按照右端点排序,相同时按照左端点排序,按照之前的排序一定 ...
- 紫书 习题8-11 UVa 1615 (区间选点问题)
这个点就是贪心策略中的区间选点问题. 把右端点从大到小排序, 左端点从小到大排序. 每次取区间右端点就可以了, 到不能覆盖的时候就ans++, 重新取点 ps:这道题不考虑精度也可以过 要着重复习一下 ...
- UVALive 2519 Radar Installation 雷达扫描 区间选点问题
题意:在坐标轴中给出n个岛屿的坐标,以及雷达的扫描距离,要求在y=0线上放尽量少的雷达能够覆盖全部岛屿. 很明显的区间选点问题. 代码: /* * Author: illuz <iilluzen ...
- UVAlive 2519 Radar Installation (区间选点问题)
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. ...
- hdu 4883 区间选点
昨天比赛的时候没有做出来,本来是想用贪心的,可是贪了好久都没有招, 今天在网上搜了解题报告~好像说这是一类区间选点问题: 有一个好的做法: (1)首先把题目中的时间全转化为分钟,那么区间就在0-144 ...
- 贪心算法----区间选点问题(POJ1201)
题目: 题目的大致意思是,给定n个闭区间,并且这个闭区间上的点都是整数,现在要求你使用最少的点来覆盖这些区间并且每个区间的覆盖的点的数量满足输入的要求点覆盖区间的数量. 输入: 第一行输入n,代表n个 ...
- UVA-1615 Highway (贪心,区间选点)
题目大意:有一条沿x轴正方向,长为L的高速公路,n个村庄,要求修建最少的公路出口数目,使得每个村庄到出口的距离不大于D. 题目分析:区间选点问题.在x轴上,到每个村庄距离为D的点有两个(超出范围除外) ...
随机推荐
- C/C++:拷贝构造函数
拷贝构造函数是一种特殊的构造函数,因为它也是用来构造对象的.它具有构造函数的所有特性.拷贝构造函数的作用是用一个已经存在的对象去初始化另一个对象,这两个对象的类类型应该是一样的.定义拷贝构造函数的形式 ...
- 用JDK自带的工具生成客户端调用Webservice的代码
JAVA下客户端调用Webservice代码简直是让人心生畏惧,今日尝试,做记录如下,参考网上的众多解决方案,下面这种方式是比较简单的. 在jdk的bin目录下有一个wsimport.exe的工具,使 ...
- uvalive 3263 That Nice Euler Circuit
题意:平面上有一个包含n个端点的一笔画,第n个端点总是和第一个端点重合,因此团史一条闭合曲线.组成一笔画的线段可以相交,但是不会部分重叠.求这些线段将平面分成多少部分(包括封闭区域和无限大区域). 分 ...
- HW6.19
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- TCP/IP协议详解内容总结
TCP/IP协议 TCP/IP不是一个协议,而是一个协议族的统称.里面包括IP协议.IMCP协议.TCP协议. TCP/IP分层: 这里有几个需要注意的知识点: 互联网地址:也就是IP地址,一般为 ...
- Camtasia Studio的安装步骤
总的来说: 1.安装 2.安装之后,进行汉化. 破解方法: 1):输入注册码安装 用户名:6-Y 注册码:GCABC-CPCCE-BPMMB-XAJXP-S8F6R 2):软件汉化 安装完成后使用汉化 ...
- Android实例-打电话、发短信和邮件,取得手机IMEI号(XE8+小米2)
结果: 1.不提示发短信卡住,点击没有反映,我猜想,可能是因为我用的是小米手机吧. 2.接收短信报错,我猜想可能是我改了里面的方法吧(哪位大神了解,求指教). 3.project -->opti ...
- 1N系列稳压二极管参数
1N系列稳压二极管参数 型号 稳定电压 型号 稳定电压 型号 稳定电压 1N5236 7.5 1N5738 12 1N6002 12 1N5237 8.2 1N5739 13 1N6003 13 1N ...
- hdoj 2083 简易版之最短距离
简易版之最短距离 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- CSS层叠样式选择器归纳
常用选择器 1.1 类型选择器:用来寻找特定类型的元素 标签 { 声明 } p { color:black; } h1 { font-weight:bold; } 1.2 后代选择器: 选择一个元 ...