poj 3069 Saruman's Army【贪心】
2016-12-05 12:19
399 查看
Saruman's Army
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
Sample Output
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;
}
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 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.
【分析】本题的关键是量取距离为<=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;
}
相关文章推荐
- 线性回归——最小二乘法_实例(二)
- C++中map容器
- 【Java笔试题】创建复数类并对复数进行运算
- Mac安装photoshop错误:安装程序未能初始化
- 线性回归——最小二乘法(二)
- linq 日常关键字使用
- 数组大折腾
- 链表奇数元素放在偶数元素前面
- 线性回归——最小二乘法_实例(一)
- angularjs自定义数据获取服务
- (转载备忘)android studio调试开发系统应用(导入修改后的的framework.jar包)
- [centos]linux搭建PostFIX和dovecot收发邮件
- Struts2+JS,Struts2+jQuery示例
- 线性回归——最小二乘法(一)
- Sql注入bug修改
- 【Jquery】jquery点击按钮弹出div层
- SMTP (二)
- 微信小程序基础之常用控件text、icon、progress、button、navigator
- 二叉树中序遍历非递归算法
- 微信小程序基础之常用控件text、icon、progress、button、navigator