Saruman ' s Army (greedy)

Source: Internet
Author: User

Saruman ' s ArmyTime limit:MS Memory Limit:65536KB 64bit IO Form At:%i64d &%i64u Submit Status Practice POJ 3069

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 his 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 ≤100 0).

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 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 (covering 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 and 70.

Code:

 #include <iostream> #include <cstring> #include <string> #include < Algorithm> #include <cstdio>using namespace std;struct fun{int l;//interval left int r;//interval right int x;//input point}a[1100];int an S;int N, r;bool cmp (struct fun A, struct fun b)//Custom comparison function, ordered from small to large on the left of the interval {return A.L<B.L;} int main () {while (~SCANF ("%d%d", &r, &n) && r! =-1 && n! =-1) {for (int i = 0; i < N        i++) {scanf ("%d", &a[i].x);            } for (int i = 0; i < n; i++) {a[i].l = A[i].x-r;        A[I].R = A[i].x+r;        } sort (A, a + N, CMP);        int L, R; ans = 1;        L = a[0].x, r = A[0].R;            for (int i = 1;i < n; i++) {if (A[I].L <= l) r = A[I].R;                else if (R < a[i].x) {ans++;                L = a[i].x;            R = A[I].R;    }} cout<< ans <<endl; } return 0;} 


Code Listing 2:

The following code copies a Daniel's.

/**  mainly examines the greedy algorithm, first, the data is sorted in ascending order, then, using the greedy algorithm to solve this problem, get the final result. */#include <iostream> #include <algorithm>using namespace std;int solve (int a[], int n,int r) {    int ans = 0, i = 0;    Sort (a,a+n);       The array is ascending    while (i < n)    {        int s = a[i++];     The point        while (I < n&&a[i] <= S + R) is i++ to the right until the distance from S is greater than R;        int k = a[i-1];     The point        while (I < n&&a[i] <= K + R) is i++ to the right until the distance from K is greater than R;        ans++;    }    return ans;} int main (void) {    int n,m;    while (cin>> n >> m,~n&&~m)    {        int a[m];        for (int i = 0; I! = m; ++i)        {            cin >> a[i];        }        cout << Solve (a,m,n) << Endl;    }    return 0;}



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.