AcWing:. 110 Sunscreen (greedy)

There sunbathing C cows, cows need to i-th minSPF [i] to maxSPF [i] between the unit light intensity.

Each cow must be painted before tanning sunscreen, sunscreen L-species, painted after the i-th, the body of the received light intensity of the SPF will stabilize [i], the i-th sunscreens have Cover [i] Bottle .

Seeking to meet the most number of cows sunbathing.

Input Format

The first line of the input integer C and L.

The next line C, in sequence each input line and a cow minSPF maxSPF values, i.e. i-th row input minSPF [i] and maxSPF [i].

The next line of the L, in sequence each input line and one kind of cover sunscreen SPF values, i.e., the i-th row input SPF [i] and the cover [i].

Separated by spaces between each row of data.

Output Format

Output an integer representing the largest number of cows to meet the cows sunbathing.

data range

1C,L25001≤C,L≤2500,
1minSPFmaxSPF10001≤minSPF≤maxSPF≤1000,
1SPF10001≤SPF≤1000

Sample input:

3 2
3 10
2 5
1 5
6 2
4 1

Sample output:

2

 

Algorithms: greedy

Solution: a structure in accordance with min_spf descending order, according to the structure b spf descending order, this is why? Because cows added current i can use any sunscreen bottles x, y (x, y in [a [i] .min_spf, a [i] .max_spf] within the range) following cows for x, y, it is possible to use It appears three conditions: first, x and y can be used. Second, x and y are not used. Third, x can be used, y can not be used. So we need to give priority to use the relatively large spf.

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

const int maxn = 2507;

struct spf {
    int min_spf, max_spf;
}a[maxn];

struct node_spf {
    int spf, cover;
}b[maxn];

bool cmp1(spf a, spf b) {
    if(a.min_spf == b.min_spf) {
        return a.max_spf > b.max_spf;
    }
    return a.min_spf > b.min_spf;
}

bool cmp2(node_spf a, node_spf b) {
    return a.spf > b.spf;
}

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; i++) {
        scanf("%d %d", &a[i].min_spf, &a[i].max_spf);
    }
    for(int i = 1; i <= m; i++) {
        scanf("%d %d", &b[i].spf, &b[i].cover);1+
    Luck (a
    }, a + n + 1, cmp1);
    sort(b + 1, b + m + 1, cmp2);
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            if(b[j].cover > 0 && a[i].min_spf <= b[j].spf && b[j].spf <= a[i].max_spf) {
                b[j].cover--;
                ans++;
                break;
            }
        }
    }
    cout << ans << endl;
    return 0;   
}

 

Guess you like

Origin www.cnblogs.com/buhuiflydepig/p/11297678.html