POJ 3069 Saruman ' s Army greedy

Source: Internet
Author: User

Saruman ' s Army

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

1#include <iostream>2#include <algorithm>3 4 using namespacestd;5 6 Const intmaxn=1005;7 8 intPOS[MAXN];9 Ten intMain () One { A     intR,n; -  -      while(cin>>r>>N) the     { -         if(r==-1&&n==-1) -              Break; -  +          for(intI=1; i<=n;i++) -Cin>>Pos[i]; +  ASort (pos+1, pos+n+1); at  -         intCount=0; -         intCur=1; -  -          while(true) -         { in             intCnt=cur; -  to              while(cnt+1<=n&&pos[cnt+1]-pos[cur]<=R) +             { -cnt++; the                 if(cnt==N) *                      Break; $             }Panax Notoginseng  -count++; theCur=CNT; +  A              while(cnt+1<=n&&pos[cnt+1]-pos[cur]<=R) the             { +cnt++; -             } $  $cur=cnt+1; -  -             if(cur>N) the                  Break; -         }Wuyi  thecout<<count<<Endl; -     } Wu  -     return 0; About}
View Code

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.

Test instructions: There are n points on the line, point I is the position of poi[i], select several from the N points, and add a tag to them.

For each point, the area within the distance of R must have a labeled point (itself with a labeled point, which can be thought to have a labeled point where the point of 0 of its distance is).

In cases where this condition is met, it is desirable to add markers for as few points as possible.

Please ask at least how many points are marked.

Greedy.

POJ 3069 Saruman ' s Army greedy

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.