Saruman ' s Army
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 5760 |
|
Accepted: 2949 |
Description
Saruman The white must leads 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 is carried by some troop in the army (i.e., Pala Ntirs is 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 Each of the Minions is within R units of some palantir.
Input
The input test file would contain multiple cases. Each test case begins with a containing a integer R, the maximum effective range of all Palantirs (w Here 0≤ R ≤1000), and a integer n, the number of troops in Saruman ' s Army (where 1≤ n ≤100 0). The next line contains n integers, indicating the positions x1, ..., xn of each troop (where 0≤ x I ≤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 is a palantir at positions and 20. Here, note this 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), Position (Coverin g positions and), position, and position 70. Here, note this palantirs must be distributed among troops and is not allowed to ' free float. ' Thus, Saruman cannot place a palantir at position-cover the troops at positions and 70.
Source
Stanford Local 2006
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <stack>5#include <Set>6#include <map>7#include <queue>8#include <algorithm>9 using namespacestd;Ten intarmy[1005]; One intMain () { A //freopen ("D:\\input.txt", "R", stdin); - intN,r; - while(SCANF ("%d%d", &r,&n)! =EOF) { the inti,j,k; - if(n==-1&&r==-1){ - Break; - } + for(i=0; i<n;i++){ -scanf"%d",&army[i]); + } ASort (army,army+n); at ints,m,e,t=0; - for(s=0; s<n;t++){ -m=s; - while(m<n&&army[m]-army[s]<=R) { -m++; - } inE=m; -m--; to while(e<n&&army[e]-army[m]<=R) { +e++; - } thes=e; * } $printf"%d\n", T);Panax Notoginseng } - return 0; the}
POJ 3069 Saruman ' s Army