Voting【Codeforces 1251 E1 && E2】【贪心】

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


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 ?1=1m1=1, ?2=2m2=2, ?3=2m3=2, ?4=4m4=4, ?5=5m5=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,55→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≤?≤2⋅1051≤t≤2⋅105) — the number of test cases.

The first line of each test case contains one integer ?n (1≤?≤2⋅1051≤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≤??≤109,0≤??<?1≤pi≤109,0≤mi<n).

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

Output

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


  题意:有N个人,他们都是投票的人,我们现在想的就是让这N个人都把票投给自己,第i个人有一个mi和pi指的是,如果有mi个人已经把票投给你了,那么他也会把票投给你,否则你可以花费pi让他把票投给你。为了让所有的人都把票投给你,问你需要的最少花费是多少?

  思路:我们从后往前看,因为0≤M≤N,所以我们从N反过来枚举所有的M,N、N-1、N-2……、i、…… 、0,如果现在是遍历到了i,那么如果我们要去贪心的选择的话,就是要让在目前的已选人数必须要求"≥i"才行,那么也就是说在待选区域的人数size应该满足 N - size ≥ i 才行。

  话说回来,我们现在还没有处理pi,pi的处理其实就好的多了,我们可以用贪心的办法解决,如果说 N - size < i,那么,我们的size一定是偏大了,我们就可以去把偏大的部分给买通了,这时候就可以用优先队列去找这size里面的最小p值即可。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
const int maxN = 2e5 + 7;
int N;
vector<int> vt[maxN];
int main()
{
    int T; scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &N);
        for(int i=0; i<=N; i++) vt[i].clear();
        for(int i=1, m, p; i<=N; i++) { scanf("%d%d", &m, &p); vt[m].push_back(p); }
        priority_queue<int, vector<int>, greater<int>> Q;
        ll ans = 0;
        for(int i=N; i>=0; i--)
        {
            int len = (int)vt[i].size();
            for(int j=0; j<len; j++) Q.push(vt[i][j]);
            while(Q.size() > N - i) { ans += Q.top(); Q.pop(); }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值