CodeForces - 1251E2 (思维+贪心)

题意

https://vjudge.net/problem/CodeForces-1251E2

一共有 n 个选民,你可以付出 pi 的代价让第 i 个选民为你投票,或者,在为你投票的人数达到 mi 时,他会主动为你投票而不用你付出任何代价。

问得到所有选民投票的最小代价。

思路

考虑贪心,对容易跟风的就跟风,对不容易跟风的就贿赂,所以对每个mi用vector插入相应的pi,然后从大到小遍历vector(不易跟风的要花钱),对于每一个mi,把对应的p插入到小根堆,小根堆里的人默认是跟风的,设小根堆大小为sz,每一次我们假设前面的人都已经把票投给你了,现在只用判断前面的人数n-sz是否大于等于当前的mi,如果是,则说明满足条件;否则要贿赂费用最少的人,将他移出小根堆(他不再跟风了,而是花钱买了)。

代码

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=200005;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
vector<ll> g[N];
int main()
{
    std::ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {

        int n;
        cin>>n;
        for(int i=0; i<n; i++)
            g[i].clear();
        for(int i=1; i<=n; i++)
        {
            ll m,p;
            cin>>m>>p;
            g[m].push_back(p);
        }
        priority_queue<ll,vector<ll>,greater<ll> >pq;
        int cnt=0;
        ll ans=0;
        for(int i=n-1; i>=0; i--)
        {
            int sz=g[i].size();
            for(int j=0; j<sz; j++)
            {
                pq.push(g[i][j]);
            }
            while(n-pq.size()<i)
            {
                ans+=pq.top();
                pq.pop();
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

  

posted @ 2019-11-13 09:45  MCQ1999  阅读(249)  评论(0编辑  收藏  举报