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;
}

 

这是一道 CodeForces 上的题目,题目编号为 749C,题目名称为 Voting。 题目描述: 有 $n$ 个人参加选举,选出一位领导人。每个人都会投票,你知道了每个人选择谁,并且可以知道选票中作废票和弃权票的数量。如果有一个人获得了半数以上的有效选票(即除去作废票和弃权票的票数),那么他将成为领导人。如果没有任何一个人获得半数以上的有效选票,则选举无效。 现在你可以修改任意数量的作废票或弃权票,使选举有效,并使你支持的候选人成为领导人。你需要最少的修改次数。 输入格式: 第一行包含三个整数 $n, a, b$,分别表示选民的数量,作废票的数量和弃权票的数量。 接下来 $n$ 行,每行包含一个字符串,表示每个人的投票情况。如果该字符串为 “YES” 或 “NO”,表示该选民对应的是弃权票;如果该字符串为 “POLL”, 表示该选民对应的是作废票;如果该字符串为其他字符串,则表示该选民对应的是有效选票,该字符串为该选民的投票对象。 输出格式: 如果无法通过修改使选举有效,则输出 -1;否则输出最少的修改次数,使得选举有效,并且你支持的候选人成为领导人。 数据范围: $1 \leq n \leq 2000, 0 \leq a, b \leq n$ 输入样例 #1: ``` 7 2 1 YES NO POLL YES YES YES NO ``` 输出样例 #1: ``` 1 ``` 样例 #1 解释: 总共有7个选民,其中弃权票有3个,作废票有2个,有效选票有2个。由于选民投票意见分散,无法确定一位领导人,因此选举无效。 我们可以将2张作废票修改为支持你所支持的候选人,这样你所支持的候选人将获得3张有效选票,超过半数,成为领导人。因此修改次数为1。 输入样例 #2: ``` 3 1 0 YES NO YES ``` 输出样例 #2: ``` 0 ``` 样例 #2 解释: 总共有3个选民,其中弃权票有1个,作废票有0个,有效选票有2个。你所支持的候选人获得了2张有效选票,超过半数,成为领导人。由于选民投票意见不分散,选举有效,无需修改任何票。因此修改次数为0。 算法1: (模拟) $O(n)$ 首先计算出除作废票和弃权票之外的票数,如果有一人的得票率超过50%,则选举有效,直接输出0。 否则,需要计算出至少需要修改多少张作废票或弃权票,才能使选举有效,并且支持你所支持的候选人成为领导人。 具体来说,我们可以考虑枚举需要修改的作废票和弃权票的数量,假设需要修改 $i$ 张作废票和 $j$ 张弃权票,使得选举有效。那么,你所支持的候选人需要得到至少 $\lceil \frac{n}{2} \rceil$ 张有效选票。我们可以通过统计当前你所支持的候选人得到的有效选票数 $cnt$,以及当前已经修改的作废票和弃权票的数量 $a'$ 和 $b'$,来判断是否存在一组解 $(i, j)$,使得选举有效。 具体来说,如果当前得票数 $cnt + i \ge \lceil \frac{n}{2} \rceil$,那么选举有效,输出 $i+j$ 即可。如果 $(\lceil \frac{n}{2} \rceil - cnt) \le a' + i \le n-b'-j$,那么也存在一组解 $(i,j)$,使得选举有效,输出 $i+j$ 即可。 如果枚举完所有情况,都无法使选举有效,那么输出 -1。 时间复杂度:$O(n^2)$ C++ 代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Wuliwuliii

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

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

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

打赏作者

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

抵扣说明:

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

余额充值