码迷,mamicode.com
首页 > 其他好文 > 详细

poj3069 Saruman's Army

时间:2016-08-17 21:05:57      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

http://poj.org/problem?id=3069

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


这道题讲的是白巫师要带领他的军队从一个地方到另外一个地方,走直线,但是为了监控他的军队嘞,他就用一种石头放在其中一个人身上,然后这个石头有r米的监控范围,问至少需要
多少石头(大概就是这个意思,我英语也不好啊[大哭])
然后就开始想代码,我想的是,中间一个人拿石头,那就可以监控两边的人啊,所以从第一个开始,一直向后……
下面是错误代码
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int r,n,a[1000];
    while(scanf("%d%d",&r,&n)==2)
    {
        if(r==-1&&n==-1)
            break;
            int sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        for(int i=1;i<n;i++)
        {
            if(a[i]-a[0]>r)
            {
                sum++;
                int k=i;
                while(a[k]-a[i]<r)
                {
                    k++;
                }
                a[0]=a[k+1];
                i=k;
                break;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

明天再看错误在哪,就是这么任性,正确的也明天再写,就是这么不简洁
相信我,以后会试着简洁的[大哭]

poj3069 Saruman's Army

标签:

原文地址:http://www.cnblogs.com/hhm324/p/5781854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!