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≤??

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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#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 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, greater> Q;
        ll ans = 0;
        for(int i=N; i>=0; i--)
        {
            int len = (int)vt[i].size();
            for(int j=0; j N - i) { ans += Q.top(); Q.pop(); }
        }
        printf("%lld\n", ans);
    }
    return 0;
}

 

你可能感兴趣的:(贪心,Codeforces)