[CF994B] Knights of a Polygonal Table - greedy, stack

There are n knights want to battle. Each Knight has the ability to value (different from each other), and who has some gold coins. If the capacity is greater than the value of A Knight Rider B, then A can kill Knight Rider B, B and get all the gold knight body. But even cruel knight will not be excessive, they will kill most k knight. For each knight, you obtain the maximum value after killing all the people he can kill (only he could kill someone else does not move) him gold.

Solution

Noted that "different from each other," this weakened condition, the ability to consider the value of small to large order, and then each knight can only kill him in front of people. So before the dynamic maintenance \ (k \) Japanese just fine

(Meaning of the questions a little fairy)

#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 100005;

struct knight {
    int p,c,ans,id;
    bool operator < (const knight &x) const {
        return c > x.c;
    }
} a[N];

int n,k,ans[N];

bool cmp(const knight &x, const knight &y) {
    return x.p < y.p;
}

signed main() {
    ios::sync_with_stdio(false);
    cin>>n>>k;
    for(int i=1;i<=n;i++) a[i].id=i;
    for(int i=1;i<=n;i++) cin>>a[i].p;
    for(int i=1;i<=n;i++) cin>>a[i].c;
    sort(a+1,a+n+1,cmp);
    priority_queue <knight> q;
    int sum=0;
    for(int i=1;i<=n;i++) {
        a[i].ans = sum;
        sum += a[i].c;
        q.push(a[i]);
        if(q.size()>k) {
            sum -= q.top().c;
            q.pop();
        }
    }
    for(int i=1;i<=n;i++) ans[a[i].id]=a[i].ans+a[i].c;
    for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
}

Guess you like

Origin www.cnblogs.com/mollnn/p/12292623.html