CodeForces - 1251E2 (thinking + greedy)

The meaning of problems

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

There are  n number of voters, you can pay  the p- i  price so that the first  i voters to vote for you, or, in the number of people vote for you up to  m i  , he will take the initiative to vote for you and you do not have to pay any price.

Q. get the minimum cost of all voters.

Thinking

Consider greedy, easy to follow the trend of the suit, the suit will not easy for bribery, the insertion of each corresponding pi mi with Vector, then descending traverse Vector (not easy to follow the trend to spend money), for each mi , the corresponding p inserted into a small heap root, root small heap of people to follow suit by default, set up a small root heap size sz, every time we assume that in front of people have voted for you, now only judge the number in front of the n-sz is greater than equal to the current mi, and if so, then the condition is satisfied; otherwise the least cost to bribe people, he would root out of a small pile (he no longer follow the trend, but rather spent the money).

Code

#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;
}

  

Guess you like

Origin www.cnblogs.com/mcq1999/p/11846907.html