tokitsukaze and Soldier 優先隊列

題目描述
在一個遊戲中,tokitsukaze需要在n個士兵中選出一些士兵組成一個團去打副本。
第i個士兵的戰力爲v[i],團的戰力是團內所有士兵的戰力之和。
但是這些士兵有特殊的要求:如果選了第i個士兵,這個士兵希望團的人數不超過s[i]。(如果不選第i個士兵,就沒有這個限制。)
tokitsukaze想知道,團的戰力最大爲多少。
輸入描述:
第一行包含一個正整數n(1≤n≤10^5)。
接下來n行,每行包括2個正整數v,s(1≤v≤10^9,1≤s≤n)。
輸出描述:
輸出一個正整數,表示團的最大戰力。
示例1
輸入
2
1 2
2 2
輸出
3
示例2
輸入
3
1 3
2 3
100 1
輸出
100
我們來想一下這個題要怎麼貪心,考慮到一個集合的士兵個數只與集合裏面的最小的s[i]有關,那麼我們就只需要枚舉這個s[i],我們就能夠得到所有的集合有可能的集合,那知道這些集合當中的s[i]之後,我只需要的就是在比s[i]大的士兵裏面尋找s[i]-1個士兵其戰鬥力和最高即可,那我們對於s[i]從大到小排序,直接用優先隊列維護!(難在貪心)

#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
#define inf 100000
typedef long long ll;
using namespace std;
struct node{
    ll ft,s;
}data[inf+10];
bool cmp(struct node a,struct node b){
    return a.s>b.s;
}
struct opr{
    bool operator()(struct node a,struct node b){
        return a.ft>b.ft;
    }
};
int main (){
    ios::sync_with_stdio(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>data[i].ft>>data[i].s;
    }
    sort(data+1,data+1+n,cmp);
    priority_queue<node,vector<node>,opr> que;
    ll mmax=0,ans=0;
    for(int i=1;i<=n;i++){
        if(que.size()<data[i].s){
            que.push(data[i]);
            ans+=data[i].ft;
            mmax=max(mmax,ans);
        }else{
            while(que.size()>=data[i].s){
                ans-=que.top().ft;
                que.pop();
            }
            que.push(data[i]);
            ans+=data[i].ft;
            mmax=max(mmax,ans);
        }
    }
    cout<<mmax<<endl;
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章