Cattle-off practice match 50 C tokitsukaze and Soldier

Cattle-off practice match 50 C tokitsukaze and Soldier

link:

https://ac.nowcoder.com/acm/contest/1080/C

Source: Cattle-off network

Title Description

In a game, tokitsukaze you need to select some of the soldiers in the n soldiers make up a group to play copy.
Combat power of the i-th soldier to v [i], combat power groups are fighting force of all the soldiers in the regiment and.
But these soldiers have a special request: If you choose the i-th soldiers, the soldiers hope the number of groups of no more than s [i]. (If you do not choose the i-th soldier, do not have this limitation.)
Tokitsukaze wondered combat power groups up to much.

Enter a description:

第一行包含一个正整数n(1≤n≤10^5)。
接下来n行,每行包括2个正整数v,s(1≤v≤10^9,1≤s≤n)。

Output Description:

输出一个正整数,表示团的最大战力。

Example 1

Entry

copy

2
1 2
2 2

Export

copy

3

Example 2

Entry

copy

3
1 3
2 3
100 1

Export

copy

100

answer:

Greedy, press s descending order, if you want to choose the i-th soldier, if the selected number has more than s [i], put people inside the selected minimum combat power removed (with a priority queue implementation) , traverse again find maxn (code is easy to understand

#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;

struct node
{
    int v, s;
}a[100010];
priority_queue<int, vector<int>, greater<int>>q;

bool cmp(node x, node y) {
    return x.s > y.s;
}
int main() {
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++) scanf("%d %d", &a[i].v, &a[i].s);
    sort(a, a + n, cmp);
    long long sum = 0, maxn = 0;
    for(int i = 0; i < n; i++) {
        sum += a[i].v;
        while(q.size() >= a[i].s) {
            int b = q.top();
            q.pop();
            sum -= b;
        }
        q.push(a[i].v);
        if(sum > maxn) maxn = sum;
    }
    printf("%lld\n", maxn);
    return 0;
}

Guess you like

Origin www.cnblogs.com/fanshhh/p/11407965.html