Saruman's Army (贪心)
来源:互联网 发布:windows扩展屏 编辑:程序博客网 时间:2024/05/22 01:33
Saruman's Army
Saruman the White must lead his army along a straight path from Isengard to Helm’s Deep. To keep track of his forces, Saruman distributes seeing stones, known as palantirs, among the troops. Each palantir has a maximum effective range of R units, and must be carried by some troop in the army (i.e., palantirs are not allowed to “free float” in mid-air). Help Saruman take control of Middle Earth by determining the minimum number of palantirs needed for Saruman to ensure that each of his minions is within R units of some palantir.
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, …, xnof each troop (where 0 ≤ xi ≤ 1000). The end-of-file is marked by a test case with R = n = −1.
For each test case, print a single integer indicating the minimum number of palantirs needed.
0 310 20 2010 770 30 1 7 15 20 50-1 -1
24
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.
题意:相当于在给定的任意某点里画半径为r的圈,求至少需要多少个圈能将所有点涵盖进去。
AC代码:
#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#define maxn 1005using namespace std;int pos[maxn];int main(){int r,n,i,j,flag,count;while(scanf("%d%d",&r,&n)!=EOF){if(r==-1&&n==-1)break;for(i=1;i<=n;++i){scanf("%d",&pos[i]);}count=0;flag=1;sort(pos+1,pos+n+1);i=1;pos[n+1]=pos[n]+r+1;while(i!=n+1){for(j=i;j<=n+1;++j){if((pos[i]+r)<pos[j]){ //<=flag=flag*(-1);if(flag<0){i=j-1;break;}else {count++;i=j;break;}}}}cout<<count<<endl;}}
- Saruman's Army(贪心)
- Saruman's Army (贪心)
- Saruman's Army 贪心
- POJ - Saruman's Army(贪心)
- poj 3069 Saruman's Army(贪心)
- POJ 3069 Saruman's Army (贪心)
- POJ3069 Saruman's Army Greedy(贪心)
- POJ 3069 - Saruman's Army(贪心)
- POJ 3069 Saruman's Army (贪心)
- POJ 3069 Saruman's Army(贪心)
- POJ 3069 Saruman's Army(贪心)
- POJ 3069.Saruman's Army(贪心)
- POJ 3069 Saruman's Army (贪心 )
- POJ - 3069 Saruman's Army(贪心)
- poj3069 Saruman's Army (贪心)
- POJ:3069 Saruman's Army(贪心)
- POJ, 3069Saruman's Army(贪心)
- POJ 3069 Saruman's Army(贪心)
- Redis安全注意事项
- Java 并发 --- CyclicBarrier源码分析
- 阿里云安装jdk-tomcat-mysql
- 前端面经-webpack
- 分答项目_知识点:h5自定义属性的jquery获取方法
- Saruman's Army (贪心)
- JavaScript 面向对象之 原型-prototype
- java详解 --- 随机数、函数及数组部分
- JPA
- Java知识点与BUG的汇总(个人整理的)以及一些代码优化的方法、项目结构剖析的描述
- java基础 3、"=="和equals方法有什么区别
- Python实现Pat 1065. A+B and C (64bit) (20)
- 权限学习 -- Spring Security数据库、缓存、自定义决策
- 11月工作总结