其他分享
首页 > 其他分享> > 牛客 tokitsukaze and Soldier(优先队列+排序)

牛客 tokitsukaze and Soldier(优先队列+排序)

作者:互联网

对于忍受程度越高的人,程度越低的能够做到,程度更高的人也能做到,因此我们按程度从大到小排序后,用优先队列维护

我们枚举每一天,将大于等于这天忍受程度都放进优先队列后,弹出小的直到满足要求

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long ll;
typedef pair<ll,ll> pll;
pll g[N];
int main(){
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    int i;
    for(i=0;i<n;i++){
        cin>>g[i].second;
        cin>>g[i].first;
    }
    sort(g,g+n);
    reverse(g,g+n);
    priority_queue<int,vector<int>,greater<int>> q;
    int j;
    ll sum=0;
    ll ans=0;
    for(i=n,j=0;i>=0;i--){
        while(j<n&&g[j].first>=i){
            q.push(g[j].second);
            sum+=g[j].second;
            j++;
        }
        while((int)q.size()>i){
            auto t=q.top();
            q.pop();
            sum-=t;
        }
        ans=max(ans,sum);
    }
    cout<<ans<<endl;
}
View Code

 

标签:队列,牛客,int,sum,tokitsukaze,Soldier,second,ans,ll
来源: https://www.cnblogs.com/ctyakwf/p/13725806.html