E2. Voting (Hard Version)-----------思维(很难)

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.

题意:有N个人,每个人有两个属性mi和pi,mi代表着如果有mi个人投票给他,那么他就会把票投给他,否则你需要花费pi的代价来收买他。请问最少花费多少使得所有人都投他。

解析:我们从N反过来枚举,如果遍历到i,用贪心的策略,必须已获得的人数>=i才可以。那么假如待选区域有size人数,那么必须满足 N-size>=i才行。
如果N-size<i 说明size 偏大 ,那么就需要把size偏大的这部分收买他,用优先队列来存size这部分的pi

思路来源:https://blog.csdn.net/qq_41730082/article/details/102741418

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+1000;
vector<int>  v[N];
int t,n,x,y;
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=0;i<=n;i++) v[i].clear();
		for(int i=1;i<=n;i++)
		{
			scanf("%d %d",&x,&y);
			v[x].push_back(y);
		}
		priority_queue<int, vector<int>, greater<int>> q;
		ll ans=0;
		for(int i=n;i>=0;i--)
		{
			for(int j=0;j<v[i].size();j++) q.push(v[i][j]);
			while(n-q.size()<i) {ans+=q.top();q.pop(); } //q.size()偏大,我们需要在里面找最小的pi,以满足贪心策略。假如现在n-q.size()=6,i=6。那么是满足的
	//	假如现在n-q.size()=5(已选),n=6 (需要的人数) 那我们就需要去找最小的pi
			
		}
		cout<<ans<<endl;
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值