【链接】
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 ≤ 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个,那么要求他在这个路段的所有广告位都要看到。输出要求的广告位的位置。
【分析与总结】
经典贪心,区间选点问题。《算法入门经典》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;
}
分享到:
相关推荐
jar包,亲测可用
jar包,亲测可用
jar包,亲测可用
标题“opencv-CFR-advertisement-system-Win”表明这是一个基于OpenCV的连续帧识别(CFR)广告系统,专为Windows平台设计。这个项目可能涉及到计算机视觉、图像处理和实时视频分析,利用Python作为主要编程语言。 ...
"Suggestive-Advertisement-Bidding-Mobile-Platform"项目正是一个聚焦于这一领域的创新实践,它利用Java编程语言构建了一个智能的、暗示性的广告出价移动平台。这个平台不仅在大数据分析的背景下发挥着重要作用,...
JMeter是一款广泛应用于性能测试的开源工具,尤其在Web应用测试方面表现突出。...标题“Jmeter图形界面”指的是JMeter的用户界面,它是进行性能测试的核心组成部分,提供了丰富的图形和表格来展示测试结果。...
根据文件内容,可以提取以下知识点: 1. 面向对象分析(OOA)中类与类之间的关系: - "IS-A"关系代表泛化关系,表明一个类是另一个类的子类,例如一个具体的汽车类是车辆类的子类。 - "IS-PART-OF"关系代表聚合...
### 无法删除嵌套文件夹解决方案 在使用MyEclipse等开发工具时,有时会因为误操作而导致项目中出现无限循环的嵌套文件夹。这种情况下,简单的删除操作往往无法解决问题,甚至会导致资源管理器卡死或命令行操作失败...
绿色、免安装、视频下载神器、idm 6.25.25,IDM 支持IE, Opera, Firefox, Chrome等所有流行的浏览器,如果启用高级集成,则可以捕获和接管从任何程序的下载。IDM的续传功能可以恢复因为断线、网络问题、计算机当机...
这个版本的Apache引入了许多改进和优化,旨在提高性能、安全性和稳定性。在Linux环境中,从源码编译Apache HTTPD是理解其工作原理、自定义配置和确保与系统组件兼容性的好方法。 首先,Apache HTTPD 2.4系列的一个...
暗示性广告出价移动平台 大数据分析 这是大数据分析课程最终项目源代码。 子目录中的 Java 文件是 Android 源代码,其中 Mahout_Source_File.java 文件包含 Mahout 推荐引擎源代码。
在微信小程序开发领域,"超级跑腿一体包案例源码"是一个典型的移动开发项目,它提供了从订单创建、任务分配到完成配送的全套解决方案。这个压缩包文件包含了开发者进行微信小程序构建所需的所有必要文件,旨在帮助...
文件说明: 这是一个dos7.1的ghost磁盘镜像文件 里面包括全套DOS,UCDOS70,使用时需要安装, cd\ucdos70 install 还包括dos版的diskgen 使用方法 ...点OK,点YES....重启电脑选择U盘启动就可以启动了.
2008年网易应聘资料(很全,包括笔试,面试题和部分答案),相当不错,冯慧军强烈推荐!!!!
大数据是当今信息技术领域的一个热门话题,它涉及到海量数据的采集、存储、处理和分析,以挖掘潜在的价值。在这个“大数据学习文档.zip”压缩包中,包含了一系列与大数据相关的技术,如MapReduce(MR)、Hive、Sqoop...
使用技术:HTML+CSS+JS 在线演示地址:https://haiyong.site/moyu/mofang/ 说明:使用 HTML CSS 和 JavaScript 创建的3D酷炫拼魔方网页游戏源码。我们在很多地方都可以用,比如适用于不同类型的个人网站、游戏站等。
在网页设计中,Canvas元素是HTML5引入的一个强大的特性,它允许开发者在浏览器上进行动态图形绘制,提供了丰富的API来创建交互式2D图形。本文将深入探讨如何在Canvas中使用外部字体,以Inconsolata.ttf为例,帮助你...
### 施乐3300维修手册核心知识点详解 #### 一、手册概述 - **手册类型**:《施乐3300维修手册》是一份详细的设备维护指南,适用于ApeosPort-III C3300/C2200/C2201以及DocuCentre-III C3300/C2200/C2201型号的...
标题中的“VB6.0 截图软件 截取全屏 截取区域”表明了这是一个使用Visual Basic 6.0(简称VB6.0)编程语言开发的屏幕截图工具,具备全屏截图和自定义区域截图两种功能。在描述中,“自己照着做的截图软件”暗示了该...
unity多人射击游戏源码Shooter IO - Multiplayer Shooting Arena 1.06 所支持的Unity版本:2017.2.0 及以上版本 ...- Advertisement Monetization - Collectibles powerups - Mobile and Keyboard input