Recommendations CodeForces - 1314A greedy

//贪心
//从初始值最小开始
//如果当前值有许多,那么就把花费最大的留下,其他的都加一个
//然后依次网上增加 
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
priority_queue<int>h;
const int N=2e5+10;
int n;
struct node {
    int w,cost;
} a[N];
bool cmp(node a,node b) {
    if(a.w==b.w)
        return a.cost<b.cost;
    return a.w<b.w;
}
int main() {
    cin>>n;
    for(int i=1; i<=n; i++)
        cin>>a[i].w;
    for(int i=1; i<=n; i++)
        cin>>a[i].cost;
    //排序
    //初始值为第一标准
    //花费为第二标准 
    sort(a+1,a+1+n,cmp);
    int nowf=-1;
    ll sum=0,ans=0;
    for(int i=1; i<=n; i++) {
        while(nowf<a[i].w) {
            if(h.empty()) {
                nowf=a[i].w;
                break;
            }
            nowf++;
            if(!h.empty()) {
                sum-=h.top();
                h.pop();
            }
            ans+=sum;
        }
        h.push(a[i].cost);
        sum+=a[i].cost;
    }
    while(!h.empty()) {
        nowf++;
        if(!h.empty()) {
            sum-=h.top();
            h.pop();
        }
        ans+=sum;
    }
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/QingyuYYYYY/p/12501703.html