Poj-3069-saruman ' s Army (Java simple greedy) __java

Source: Internet
Author: User
Saruman ' s Army
Time Limit: 1000MS Memory Limit: 65536K
Total submissions: 4699 accepted: 2430

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 to troop in 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 Each of he 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 a 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 of
7
1 7
-1-1

Sample Output

2
4

Hint

In the ' the ' in the ' the ' in the ' the ' the ' the ' the ' Palantir ' in positions and 20. Saruman Here, this is a single Palantir with range 0 can cover both to 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 Positions (g), position, and position 70. Here, note that Palantirs must is distributed among troops and are not allowed to "free float." Thus, Saruman cannot place a palantir in position to cover the troops at positions and 70.

Source Stanford Local 2006


First of all explain the title (Bo main English slag. (translated from the Challenge Process design contest): There are n dots on the line. The position of point I is XI. Select several from these n points and tag them. For each point, the area within R must have a marked point (its own marked point, which can be thought to have a marked point with a distance of 0). When this condition is met, you want to add tags to as few points as possible. How many points would you like to be labeled at least?
Restrictions: 1.1<=n<=1000 2.  0<=r<=1000 3. 0<=xi<=1000
Sample Example:
Input: r=10 n=6 x={1,7,15,20,30,50}
Output: 3

 * * from the left-most point of view, for this point, to the area within the radius of R must have a marked point.
 * This point is at the far left, and obviously the marked point must be to the right of this point. So start at the left-most point, distance
 * is the farthest point within R, for the point where the symbol is added to the right beyond the next point of R, you can think of this point as the
 first leftmost point, because the two points are equivalent, that is, the same method can be used.
 * *

import java.io.*;
Import java.util.*;

public class Main
{public

	static void Main (string[] args)
	{
		//TODO auto-generated method stub
		Scanner input = new Scanner (system.in);
		while (Input.hasnext ())
		{
			int R = Input.nextint ();
			int N = Input.nextint ();
			int x[] = new Int[n];
			for (int i = 0; i < N; i++)
			{
				X[i] = Input.nextint ();
			}
			Arrays.sort (X);
			int i = 0, ans = 0;
			while (I < N)
			{
				int s = x[i++];
				Keep to the right until the distance from S is greater than the point while
				(i < N && X[i] <= s + R)
					i++;
				P is the position of the new tagged point
				int p = x[i-1];
				Keep to the right until the distance from P is greater than the point while
				(i < N && X[i] <= p + r)
					i++;
				ans++;
			}
			System.out.println (ANS);}}




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.