POJ 3069 Saruman's Army 贪心
2015-10-29 13:04
483 查看
Saruman's Army
minions Input The input test file will contain multiple cases. Each test case begins with a single line containing an integer R, the maximum effective range of all palantirs (where 0 ≤R ≤ 1000), and an integer n, the number of troops in Saruman’s army (where 1 ≤ n ≤ 1000). The next line contains n integers, indicating the positions x1, …, xn of each troop (where 0 ≤ xi ≤ 1000). The end-of-file is marked by a test case with R = n = −1. Output For each test case, print a single integer indicating the minimum number of palantirs needed. Sample Input 0 3 10 20 20 10 7 70 30 1 7 15 20 50 -1 -1 Sample Output 2 4 Hint In the first test case, Saruman may place a palantir at positions 10 and 20. Here, note that a single palantir with range 0 can cover both of the troops at position 20. In the second test case, Saruman can place palantirs at position 7 (covering troops at 1, 7, and 15), position 20 (covering positions 20 and 30), position 50, and position 70. Here, note that palantirs must be distributed among troops and are not allowed to “free float.” Thus, Saruman cannot place a palantir at position 60 to cover the troops at positions 50 and 70. Source Stanford Local 2006 |
[Discuss]
抽象成一维数轴上的若干个点,标记一个点那么这个点可以辐射到len范围(左右都可以)距离内的点。给出点的影响范围len问如何标记最少的点使得全部的点都被辐射到。
先从最左边的点找到离它最远且能辐射到它的点,然后找到这个点能影响到的最右边的点,这样就找的了一个点然后类推。
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制 #pragma comment(linker, "/STACK:102400000,102400000")//手工开栈 #include <map> #include <set> #include <queue> #include <cmath> #include <stack> #include <cctype> #include <cstdio> #include <cstring> #include <stdlib.h> #include <iostream> #include <algorithm> #define rd(x) scanf("%d",&x) #define rd2(x,y) scanf("%d%d",&x,&y) #define rds(x) scanf("%s",x) #define rdc(x) scanf("%c",&x) #define ll long long int #define maxn 100005 #define mod 1000000007 #define INF 0x3f3f3f3f //int 最大值 #define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i) #define MT(x,i) memset(x,i,sizeof(x)) #define PI acos(-1.0) #define E exp(1) using namespace std; int len,n,cnt,st,ed; int a[maxn]; int main(){ while(rd2(len,n)&&(len+n!=-2)){ FOR(i,1,n)rd(a[i]); sort(a+1,a+1+n);cnt=0; FOR(i,1,n){ st=a[i]; while(i<=n&&a[i]<=st+len)i++; ed=a[i-1]; while(i<=n&&a[i]<=ed+len)i++; i--; cnt++; } printf("%d\n",cnt); } return 0; }
相关文章推荐
- Log4j2在WEB项目中配置
- Java编程思想 - 第7章、复用类
- 删除文件后,磁盘可用空间并没有释放怎么办?
- Andriod控件PopupWindow嵌套Listview
- iOS笔试题
- 【转】Linux网络编程入门
- BarCode Reader SDK使用教程:如何从图像中读取条码
- OAuth 2.0 授权原理
- 【FastDev4Android框架开发】AndroidAnnnotations注入框架使用之注入组件Components(九)
- 河南第八届省赛(挑战密室)
- 解决Xcode7.1插件安装的办法
- javascript: detect mobile devices or browser
- KL Divergence between two multivariate normal distributions(使用了trace 和Expection的性质)
- IP|子网|子网掩码| 域名|DNS
- xampp里mysql数据库结构一直加载,如何在mysql页面创建自增id
- 5.7 Pairwise Swap
- c#读写App.config,ConfigurationManager.AppSettings 不生效的解决方法
- ubuntu kylin 中的一些个人使用技巧
- objective学习笔记1
- mysql 如何用一条SQL将一张表里的数据插入到另一张表 3个例子