码迷,mamicode.com
首页 > 其他好文 > 详细

Recommendations CodeForces - 1314A 贪心

时间:2020-03-16 09:18:44      阅读:55      评论:0      收藏:0      [点我收藏+]

标签:pac   pop   comm   name   turn   sort   algorithm   sum   include   

//贪心
//从初始值最小开始
//如果当前值有许多,那么就把花费最大的留下,其他的都加一个
//然后依次网上增加 
#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;
}

Recommendations CodeForces - 1314A 贪心

标签:pac   pop   comm   name   turn   sort   algorithm   sum   include   

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12501703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!