Educational Codeforces Round 75 (Rated for Div. 2) E

The meaning of problems

Some people say to convince
everyone can use the money to buy, or to reach persuaded m i m_i Human condition

answer

We can consider m i = n 1 m_i=n-1 , when only one person can reach a condition, then I must leave p i p_i biggest.
So, when m m i m \ geq m_i When able to reach conditions may have n m i n-m_i personal.
In order to meet all the inequality, we pushed backwards, and more than once n m i n-m_i We removed from p i p_i The minimum buy.

#include<bits/stdc++.h>
#define FOR(i,l,r) for(int i=l;i<=r;i++)
#define sf(x) scanf("%d",&x)
typedef long long ll;
using namespace std;

const ll mod = 1e9+7;
const int maxn = 500050;

vector<int>G[maxn];
int n,p[maxn],m[maxn];

int main(){
    int T;cin>>T;
    while(T--){
        scanf("%d",&n);
        FOR(i,1,n)scanf("%d%d",&m[i],&p[i]);
        FOR(i,0,n-1)G[i].clear();
        FOR(i,1,n)G[m[i]].push_back(p[i]);
        priority_queue<int,vector<int>,greater<int> >q;
        ll ans=0;
        for(int i=n-1;i>=0;i--){
            for(auto x:G[i])q.push(x);
            while(q.size()>n-i){
                ans+=q.top();
                q.pop();
            }
            //cout<<q.size()<<endl;
        }
        printf("%lld\n",ans);
    }
}

Published 203 original articles · won praise 17 · views 20000 +

Guess you like

Origin blog.csdn.net/mxYlulu/article/details/104248430