【CodeForces 1251E2 --- Voting [Hard Version]】

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_41879343/article/details/102764753

【CodeForces 1251E2 --- Voting [Hard Version]】


Description

The only difference between easy and hard versions is constraints.

Now elections are held in Berland and you want to win them. More precisely, you want everyone to vote for you.

There are n voters, and two ways to convince each of them to vote for you. The first way to convince the i-th voter is to pay him pi coins. The second way is to make mi other voters vote for you, and the i-th voter will vote for free.

Moreover, the process of such voting takes place in several steps. For example, if there are five voters with m1=1, m2=2, m3=2, m4=4, m5=5, then you can buy the vote of the fifth voter, and eventually everyone will vote for you. Set of people voting for you will change as follows: 5→1,5→1,2,3,5→1,2,3,4,5.

Calculate the minimum number of coins you have to spend so that everyone votes for you.

Input

The first line contains one integer t (1≤t≤2⋅105) — the number of test cases.

The first line of each test case contains one integer n (1≤n≤2⋅105) — the number of voters.

The next n lines contains the description of voters. i-th line contains two integers mi and pi (1≤pi≤109,0≤mi<n).

It is guaranteed that the sum of all n over all test cases does not exceed 2⋅105.

Output

For each test case print one integer — the minimum number of coins you have to spend so that everyone votes for you.

Sample Input

3
3
1 5
2 10
2 8
7
0 1
3 1
1 1
6 1
1 1
4 1
4 1
6
2 6
2 3
2 8
2 7
4 4
5 5

Sample Output

8
0
7

Note

In the first test case you have to buy vote of the third voter. Then the set of people voting for you will change as follows: 3→1,3→1,2,3.

In the second example you don’t need to buy votes. The set of people voting for you will change as follows: 1→1,3,5→1,2,3,5→1,2,3,5,6,7→1,2,3,4,5,6,7.

In the third test case you have to buy votes of the second and the fifth voters. Then the set of people voting for you will change as follows: 2,5→1,2,3,4,5→1,2,3,4,5,6.

解题思路:

当有x个人都需要其他人中有m个人投票才免费投票的时候,我们需要考虑:
n-x<m时代表m个人中有一部分我们必须给钱,因为没法满足有m个人投票才免费投票。所以我们需要去买其中一部分p[i]比较低的人的票。
所以只需从后往前遍历,判断n-x<m,如果n-x<m成立,那么我们就花钱买。直到不成立。

AC代码:

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define SIS std::ios::sync_with_stdio(false)
typedef long long ll;
const int MAXN = 200005;
vector<ll> v[MAXN];
priority_queue<ll,vector<ll>,greater<ll> > q;

int main()
{
    SIS;
    int T;
    cin >> T;
    while(T--)
    {
        int n,x,y;
        cin >> n;
        for(int i=0;i<=n;i++) v[i].clear();
        while(!q.empty()) q.pop();
        for(int i=0;i<n;i++)
        {
            cin >> x >> y;
            v[x].push_back(y);
        }
        ll ans=0;
        for(int i=n;i>=0;i--)
        {
            for(auto xx : v[i]) q.push(xx);
            while(q.size()>n-i)
            {
                ans+=q.top();
                q.pop();
            }
        }
        cout << ans << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41879343/article/details/102764753