poj 3069 Saruman's Army【贪心】
来源:互联网 发布:stc52单片机键盘原理图 编辑:程序博客网 时间:2024/06/05 13:28
Description
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.
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 310 20 2010 770 30 1 7 15 20 50-1 -1
Sample Output
24
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.
【分析】本题的关键是量取距离为<=R的点,代码中有俩while循环,需慢慢品味其奥妙的地方
PS:抓紧时间看挑战(《挑战程序设计竞赛》)书去,里面东西对于我这菜鸟来说太需要了。
AC代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAX_N = 1005;int N, R;int X[MAX_N];void solve(){sort(X, X+N);int i = 0, ans = 0;while(i < N){//s是没有被覆盖的最左的点的位置int s = X[i++];//一直向右前进直到距s的距离大于R的点while(i < N && X[i] <= s + R) i++;//p是新加上标记的点的位置int p = X[i-1];//一直向右前进直到距p的距离大于R的点while(i < N && X[i] <= p + R) i++;ans++; }printf("%d\n", ans);}int main(){while(scanf("%d %d", &R, &N) != 1){if(R == -1 && N == -1) break;for(int i = 0; i < N; i++)scanf("%d", &X[i]);solve();}return 0;}
- poj 3069 Saruman's Army 贪心
- poj 3069Saruman'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 (贪心)
- 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 【贪心】
- POJ 3069 Saruman's Army 贪心
- POJ 3069 Saruman's Army(贪心)
- POJ 3069 Saruman's Army(贪心)
- 数组大折腾
- 【项目】JumpRope-History Data
- Unix下管道实现进程间通信
- oracle的物理存储结构
- Sql注入bug修改
- poj 3069 Saruman's Army【贪心】
- 二叉树中序遍历非递归算法
- 海量数据处理之Bti-map详解
- (转载备忘)android studio调试开发系统应用(导入修改后的的framework.jar包)
- 链表奇数元素放在偶数元素前面
- Mac安装photoshop错误:安装程序未能初始化
- 数组大折腾
- 浏览器发送cookie规则
- 01背包(简单dp) HDU 1203 I need Offer