Problem: Professional Network (greedy)

topic

Portal

Thinking

First, it should be clear
that we buy because this person is not to meet his demands, but because he was cheap
for \ (a_i == n \) case of
those who do not want to put out and be your friend
so you have to take the money buy
consideration to solve the \ (a_i \) needs
to address (a_i \) \ needs when
all \ (a_j <a_i \) of j has been resolved
remaining only from \ (a_i <a_k \) in get
after, so to solve

Code

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
int n;
int tot;
int ans;
vector<int> v[200005];
priority_queue<int,vector<int>,greater<int> > q;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int a,b;
        cin>>a>>b;
        v[a].push_back(b);
    }
    for(int i=0;i<v[n].size();i++)
    {
        ans=ans+v[n][i];
    }
    tot=n;
    for(int i=n-1;i>=1;i--)
    {
        tot=tot-v[i].size();
        for(int j=0;j<v[i].size();j++)
            q.push(v[i][j]);
        while(tot<i)
        {
            ans+=q.top();
            q.pop();
            tot++;
        }
    }
    cout<<ans;
    return 0;
}

Guess you like

Origin www.cnblogs.com/loney-s/p/11838436.html