Saruman ' s Army

Source: Internet
Author: User

Part I: Topics

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

Saruman ' s Army
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6839 Accepted: 3516

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.

Part II: Ideas

Greedy: 1, from small to large sort. Starting at the far left, starting with an not contained point and looking back at a point as the center, ensure that the circle with the radius of R must contain a starting point.

2, look for the nearest point with the center but at the outside of the circle as a new starting point, and then find the center.

3, repeat until the far right.

4, Note: 1, there may be duplicate points that can be sorted when going heavy. (This is used to "de-weigh" the distance as an integer when comparing distances).

2, if the last point is a new starting point, that is, to the center of their own.

Part III: Code

#include <iostream>#include<algorithm>using namespacestd;intMain () {intn,m; intarmy[1001];  while(cin>>m>>n,m!=-1&&n!=-1)//when both N and M are-1 o'clock ends    {         for(intI=0; i<n;i++) {cin>>Army[i]; } sort (Army,army+N);//sort from small to large        intCount=0; //starting from the far left, take a non-contained point as a starting point, and find as many points as possible//as the center of the circle. But the starting point must be included.        When you find the center, you need to find the next starting point. //This starting point must be outside the circle.         inttemp=army[0]; //Flag 1 To find the starting point        intflag=1;  for(intI=1; i<n;i++)        {            //when the starting point exists and the current point does not contain a starting point, then the previous point is the center of the Circle            if(flag&&army[i]-temp>0&&army[i]-temp>m) {count++; Temp=army[i-1];//Center                if(army[i]-temp>m)//The current point cannot be enclosed by a circle{Temp=army[i];//take this point as a new starting point                }                Else{flag=0;//no new starting point has been found since the center was found                    Continue; }            }            if(flag==0&&army[i]-temp>m) {temp=Army[i]; Flag=1; }        }        if(flag)//if flag is 1, it indicates a new starting point, and this is the last point. Give it a mark .{Count++; } cout<<count<<Endl; }    return 0;}

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.