D. Recommendations

题目链接:https://codeforces.com/contest/1315/problem/D

题目大意:

给你 n 个数以及给这些数 +1 操作的代价,问你使得所有数字都不一样的最小代价

想法:

如果有 2 个相同的数,一定是把花费较小的那个数 +1 ,如果有 3 个相同的数,一定是把花费较小的那 2 个数 +1。

这里得先把数排个序(从小到大) 然后再利用优先队列的性质去贪就可以了,每次弹掉最大的数

#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <math.h>
#include <cstdio>
#include <iomanip>
#include <time.h>
#include <bitset>
#include <cmath>
#include <sstream>
#include <iostream>

#define LL long long
#define ls nod<<1
#define rs (nod<<1)+1
#define pii pair<int,int>
#define mp make_pair
#define pb push_back

const double eps = 1e-10;
const int maxn = 2e5 + 10;
const LL mod = 1e9 + 7;
const LL INF = 1e18;

int sgn(double a){return a < -eps ? -1 : a < eps ? 0 : 1;}
using namespace std;

#define N 200005
struct Node
{
    int x,y;
};
Node a[N];
int n;
bool cmp(Node x,Node y)
{
    return x.x<y.x;
}
signed main()
{
    cin>>n;
    for(int i=1;i<=n;i++) cin >> a[i].x;
    for(int i=1;i<=n;i++) cin >> a[i].y;
    sort(a+1,a+n+1,cmp);
    priority_queue<int> q;
    int cur=0,cnt=0;
    LL ans=0,tmp=0;
    while(!q.empty()||cur<n)
    {
        cnt++;
        if(q.empty())
        {
            cnt=a[cur+1].x;
            while(cnt==a[cur+1].x&&cur<n)
            {
                cur++;
                tmp+=a[cur].y;
                q.push(a[cur].y);
            }
        }
        else
        {
            while(cnt==a[cur+1].x&&cur<n)
            {
                cur++;
                tmp+=a[cur].y;
                q.push(a[cur].y);
            }
        }
        tmp-=q.top(); q.pop();
        ans+=tmp;
    }
    cout<<ans<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/-Ackerman/p/12426567.html