天天看點

poj 3069 Saruman's Army(貪心)



saruman‘s army

time limit: 1000ms

memory limit: 65536k

total submissions: 3446

accepted: 1752

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 palantir has a maximum effective range of r units,

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 is within r units

of some palantir.

input

the input test file will 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

sample output

hint

in the first test case, saruman may place a palantir at positions 10 and 20. here, note that 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 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 palantir at position 60 to cover the troops at positions 50 and 70.

source

簡單的一道貪心算法題,感覺英文題讀起來還是有壓力啊,讀半天讀不懂題意,讀懂題意感覺思路就很簡單了;

題意就是給你n個點,在r距離内要有點被标記,要求最小的标記點數,利用貪心算法的思路,從最左邊沒标記的點開始,找到距離r最近的那個點然後标記這個點,标記了第一個點之後,對标記的點之後r距離的第一個點标記,如此進行下去,直到最後一個點。

下面是ac的代碼: