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.
Example
inputCopy
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
outputCopy
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.
题目大意:每一个选民有两个值,跟风值m表示如果已经有m个人投票则他也会投票,贿赂值p代表p金就能使他投票,某个人想要全部人都为他投票,求最小花费。
思路:这批选民可以分为两个集合,第一个集合为需要用钱贿赂的人,第二个集合为跟风投票的人。
假设需要用钱贿赂i个人,如果当前i>=m,则跟风值为m的暂时放入另一个集合S:如果i<n-S.size,那就表示没有足够的人来贿赂,我们需要从S集合中挑选人来贿赂,这当然是调贿赂值小的来贿赂。如果i>=n-S.size,那就表示人数盈余,还可以继续将人放进S集合。
用贪心的思想,当然是贿赂的人越少越好,所以从n->0遍历需要贿赂的人,找到满足条件的即可。
还有个小问题,第i-1的贿赂会回来影响第i的贿赂吗?
显然不会,第i次贿赂已经将人分成了两个部分(需要贿赂的和必要时可以贿赂的),则不论i-1的情况需要从两个部分如何改变,他们的总和是不变的。
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #include<cmath> #include<vector> #include<queue> #define ll long long const int inf=0x3f3f3f3f; using namespace std; const int N=2e5+5; vector<int>vec[N]; priority_queue<int,vector<int>,greater<int>>q; int t,m,p,n; int main() { scanf("%d",&t); while(t--) { while(!q.empty()) q.pop(); scanf("%d",&n); for(int i=0;i<=n;i++) vec[i].clear(); for(int i=1;i<=n;i++) { scanf("%d%d",&m,&p); vec[m].push_back(p);//需要p金来收买或者至少m人投票才投票的人。 } ll ans=0; for(int i=n;i>=0;i--)//我们要用钱来收买的人数. { for(int j=0;j<vec[i].size();j++) { int temp=vec[i][j]; q.push(temp);//暂时不必收买的人数. } while(i>n-q.size())//需要的人数大于实际可以支配的人,则要从暂时不需要买的人中收买一点人出来. { ans+=q.top(); q.pop(); } } printf("%lld ",ans); } return 0; }