[Poj 3069] Saruman's Army, pojsaruman
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 palanidl has a maximum valid tive rangeRUnits, 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 isRUnits of some palantio.
Input
The input test file will contain multiple cases. Each test case begins with a single line containing an integerR, The maximum valid tive range of all palantirs (where 0 ≤R≤ 1000), and an integerN, The number of troops in Saruman's army (where 1 ≤N≤ 1000). The next line contains n integers, indicating the positionsX1 ,...,XnOf each troop (where 0 ≤Xi≤ 1000). The end-of-file is marked by a test caseR=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 palantioat positions 10 and 20. Here, note that a single palantiowith 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 palantioat position 60 to cover the troops at positions 50 and 70.
Question: Returns the corresponding coordinates of n points on a one-dimensional straight line, and marks as few points as possible in n points with a parameter-distance R, so that any point in n points can, there are marked points within the R Distance.
Question:First, consider from the leftmost. Obviously, the first vertex marked from the left is on the right of the leftmost vertex. Select the farthest point from R. Next, mark the next vertex of the rightmost vertex that can be affected by the last mark as the leftmost vertex.
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 int ans; 7 int main(){ 8 //ios::sync_with_stdio(false); 9 int n,r,x[1005];10 while(scanf("%d%d",&r,&n)&&!(r==-1&&n==-1)){11 for(int i=0;i<n;i++)cin>>x[i];12 sort(x,x+n);13 int i=0;ans=0;14 while(i<n){15 int s=x[i++];16 while(i<n&&x[i]<=s+r) i++;17 int p=x[i-1];18 while(i<n&&x[i]<=p+r) i++;19 ans++;20 }21 printf("%d\n",ans);22 }23 return 0;24 }