POJ 3069 Saruman ' s Army

Source: Internet
Author: User

                                               Saruman‘s Army                        Time Limit: 1000MS  

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 have a maximum effective range of R units, and must be carried by some troop in the army (i.e., Palantirs 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 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
Ten
7
1 7 (+)
-1-1
Sample Output
2
4
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 (cov ering positions, 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.

Title analysis: The approximate meaning of the topic can be understood as a number on a number of n points, and then let you draw as little as possible round so that the N points are falling in the circle, give the radius of the circle R;
Algorithm design: We start with the first point (position is I) as the marker bit, starting from I loop, and then one of the following points for the center of the circle to see if we can include the first point, as long as we can, we continue to look forward until we find a point s, so that the distance of S-i is greater than R. If this is the position of the s itself, then we can add one to the counter and record it once. If not, then we start with S, looking for points that can be included in the center of S-1. Finally, the point not inside the circle is T, and it is assigned to I as the starting point for the next loop. Until all points have been processed.
Algorithmic interpretation: This problem uses the greedy thought. Starting from the left, each time you find a marker that can contain the most points until all points are overwritten.

#include <cstdio>#include <algorithm>#include <iostream>using namespace STD;inta[ the];intMain () {intR,n,sum,s,t,i; while(scanf("%d%d", &r,&n) && r!=-1&& n!=-1)    { for(i=0; i<n;i++)scanf("%d", &a[i]);        Sort (a,a+n); I=0; sum=0; while(i<n) {s=i; while(A[S]-A[I]&LT;=R)            {s++; }if(s==i-1) {sum++;            i++; }Else{t=s; while(a[t]-a[s-1]&LT;=R) {t++;                } sum++;            i=t; }        }printf("%d\n", sum); }return 0;}

POJ 3069 Saruman ' s Army

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.