Debate CodeForces - 1070F (贪心)
Elections in Berland are coming. There are only two candidates — Alice and Bob.
The main Berland TV channel plans to show political debates. There are nn people who want to take part in the debate as a spectator. Each person is described by their influence and political views. There are four kinds of political views:
- supporting none of candidates (this kind is denoted as "00"),
- supporting Alice but not Bob (this kind is denoted as "10"),
- supporting Bob but not Alice (this kind is denoted as "01"),
- supporting both candidates (this kind is denoted as "11").
The direction of the TV channel wants to invite some of these people to the debate. The set of invited spectators should satisfy three conditions:
- at least half of spectators support Alice (i.e. 2⋅a≥m2⋅a≥m, where aa is number of spectators supporting Alice and mm is the total number of spectators),
- at least half of spectators support Bob (i.e. 2⋅b≥m2⋅b≥m, where bb is number of spectators supporting Bob and mm is the total number of spectators),
- the total influence of spectators is maximal possible.
Help the TV channel direction to select such non-empty set of spectators, or tell that this is impossible.
Input
The first line contains integer nn (1≤n≤4⋅1051≤n≤4⋅105) — the number of people who want to take part in the debate as a spectator.
These people are described on the next nn lines. Each line describes a single person and contains the string sisi and integer aiai separated by space (1≤ai≤50001≤ai≤5000), where sisi denotes person's political views (possible values — "00", "10", "01", "11") and aiai — the influence of the ii-th person.
Output
Print a single integer — maximal possible total influence of a set of spectators so that at least half of them support Alice and at least half of them support Bob. If it is impossible print 0 instead.
Examples
6
11 6
10 4
01 3
00 3
00 7
00 9
22
5
11 1
01 1
00 100
10 1
01 1
103
6
11 19
10 22
00 18
00 29
11 29
10 28
105
3
00 5000
00 5000
00 5000
0
Note
In the first example 44 spectators can be invited to maximize total influence: 11, 22, 33 and 66. Their political views are: "11", "10", "01" and "00". So in total 22 out of 44 spectators support Alice and 22 out of 44 spectators support Bob. The total influence is 6+4+3+9=226+4+3+9=22.
In the second example the direction can select all the people except the 55-th person.
In the third example the direction can select people with indices: 11, 44, 55 and 66.
In the fourth example it is impossible to select any non-empty set of spectators.
题意:
有四种投票立场的人,分别是 11 , 10 ,01 ,00,
11代表这个人对a和b都赞同,10代表赞同a,不赞同b,01代表不赞同a,赞同b。00表示都不赞同。
而每一个人又有一个影响力,
现在要你从给出的人中选出一些人,使之在满足投票a和投票b的票数占总人数的一半,即2*num(a或b)>=sum_num,全部选择的人的影响力的sum和是尽可能的大。
思路:
贪心策略。
我们容易知道,选择11类型的一定是百利无一害的,那么我们把11类型的所有人都全选。
我们记a的票数是cnta,b的票数是cntb,选择的总人数是sum。
那么全选择了11类型的人之后,cnta=cntb=sum,。
然后我们分析一下选择10和01的人对票数的影响。
假设我们已经选了2个11类型的人,
cnta=cntb=sum=2
然后如果选择一个01的人和一个10的人
cnta=cntb=3,sum=4
再来两个
cnta=cntb=4,sum=6
再来
cnta=cntb=5,sum=8
再来
cnta=cntb=6,sum=10
显然这是一个无限接近于1:2的比例关系,但是不会小于1:2,那么就说无论咋取都是满足条件。
而我们如果只选择01和10的人呢?
如果当前是cnta=cntb=2,sum=2,
我们只选择01一个
cnta=2,cntb=3,sum=3,
两个
cnta=2,cntb=4,sum=4,
三个
cnta=2,cntb=5,sum=5,
显然这个已经不满足条件了。
再来看看我们如果只选择00的
一个
cnta=2,cntb=2,sum=3,
两个
cnta=2,cntb=2,sum=4,
三个
cnta=2,cntb=2,sum=5,
显然也不满足条件了。
我们是否发现了什么规律呢?
如果不是同时选10和01各取一个,而是只单选01或者10中的任意一个,对接近不满足条件的贡献是和00一样的,
因为当a和b只需要一个不满足条件,整体就不满足条件了,那么10单一起和00就是一样的了,
那么回到我们的所求,
求的是让影响力最大,那么我们只需要把11的全选,01和10尽量大的各选取尽量多的之后,把01或者10还剩下的部分和00放在一个,
然后从剩余部分选择最大影响力的人,直至不符合条件。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
pii temp;
struct cmp
{
bool operator()(const pii p1,const pii p2)
{
return p1.se<p2.second;
}
};
priority_queue<pii,std::vector<pii>,cmp > heap[];
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
gbtb;
cin>>n;
repd(i,,n)
{
cin>>temp.fi>>temp.se;
heap[temp.fi].push(temp);
}
ll ans=0ll;
ll l=0ll;
ll r=0ll;
ll sum=0ll;
while(!heap[].empty())
{
ans+=heap[].top().se;
sum+=1ll;
r+=1ll;
l+=1ll;
heap[].pop();
}
while(*(l+)>=(sum+)&&(*(r+))>=(sum+)&&(!heap[].empty())&&(!heap[].empty()))
{
ans+=heap[].top().se;
ans+=heap[].top().se;
sum+=2ll;
r+=1ll;
l+=1ll;
heap[].pop();
heap[].pop();
}
while(!heap[].empty())
{
heap[].push(heap[].top());
heap[].pop();
}
while(!heap[].empty())
{
heap[].push(heap[].top());
heap[].pop();
}
while(2ll*l>=(sum+)&&2ll*r>=(sum+)&&(!heap[].empty()))
{
ans+=heap[].top().se;
heap[].pop();
sum++;
}
cout<<ans<<endl; return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Debate CodeForces - 1070F (贪心)的更多相关文章
- F - Debate CodeForces - 1070F 思维
题目链接:https://vjudge.net/problem/CodeForces-1070F 具体思路:首先把所有的00放进去,然后对于10 和01 的取两个数目最小值t,排完序后将前t个加起来, ...
- CodeForces - 893D 贪心
http://codeforces.com/problemset/problem/893/D 题意 Recenlty Luba有一张信用卡可用,一开始金额为0,每天早上可以去充任意数量的钱.到了晚上, ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 828D) - 贪心
Arkady needs your help again! This time he decided to build his own high-speed Internet exchange poi ...
- CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作
题目链接:http://codeforces.com/problemset/problem/93/B B. End of Exams time limit per test 1 second memo ...
- C - Ordering Pizza CodeForces - 867C 贪心 经典
C - Ordering Pizza CodeForces - 867C C - Ordering Pizza 这个是最难的,一个贪心,很经典,但是我不会,早训结束看了题解才知道怎么贪心的. 这个是先 ...
- Codeforces 570C 贪心
题目:http://codeforces.com/contest/570/problem/C 题意:给你一个字符串,由‘.’和小写字母组成.把两个相邻的‘.’替换成一个‘.’,算一次变换.现在给你一些 ...
- Codeforces 732e [贪心][stl乱搞]
/* 不要低头,不要放弃,不要气馁,不要慌张 题意: 给n个插座,m个电脑.每个插座都有一个电压,每个电脑都有需求电压. 每个插座可以接若干变压器,每个变压器可以使得电压变为x/2上取整. 有无限个变 ...
- Codeforces 721D [贪心]
/* 不要低头,不要放弃,不要气馁,不要慌张. 题意: 给一列数a,可以进行k次操作,每次操作可以选取任意一个数加x或者减x,x是固定的数.求如何才能使得这个数列所有数乘积最小. 思路: 贪心...讨 ...
随机推荐
- Android多Module下的Application引用方式
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 Android开发时,Application一般都放在APP中,Lib模块如果想引用Application则需要在APP中进行传递, ...
- Virtual Box虚拟机Ubuntu18.X系统安装及Mysql基本开发配置
Linux简介 什么是 Linux? Linux:世界上不仅只有一个 Windows 操作系统,还有 Linux.mac.Unix 等操作系统.桌面操作系统下 Windows 是霸主,而 Linux ...
- 深入解析ThreadLocal 详解、实现原理、使用场景方法以及内存泄漏防范 多线程中篇(十七)
简介 从名称看,ThreadLocal 也就是thread和local的组合,也就是一个thread有一个local的变量副本 ThreadLocal提供了线程的本地副本,也就是说每个线程将会拥有一个 ...
- 痞子衡嵌入式:恩智浦MCU安全加密启动一站式工具NXP-MCUBootUtility用户指南
NXP MCU Boot Utility English | 中文 1 软件概览 1.1 介绍 NXP-MCUBootUtility是一个专为NXP MCU安全加密启动而设计的工具,其特性与NXP M ...
- cocos creator主程入门教程(五)—— 日志系统
五邑隐侠,本名关健昌,10年游戏生涯,现隐居五邑.本系列文章以TypeScript为介绍语言. 这一篇介绍日志系统的设计.一般我们开发一个demo,只会简单的用cocos提供的cc.log打印下日志, ...
- [转]Uipath、BluePrism、AA产品对比之设计器篇
本文转自:https://www.jianshu.com/p/53d0d33a1a35 版本说明: Uipath V2018.3.2,BluePrism V6.3,Automation Anywher ...
- Web项目也能一键打包Android、IOS
随着移动互联网的不断发展,智能手机配置的不断提高,越来越多的年轻人基本都在使用手机,如微信.支付宝等等.已基本成为一种习惯,坐电梯也好.吃饭也好.开车也好,基本都捧着一个手机在那按来按去,开车就不建议 ...
- [TCP/IP] 网络层-简单查看路由表
使用抓包工具排除网络故障:1.如果一台计算机在网络上发广播包,广播的mac地址是全ff,就有可能堵塞2.使用抓包工具,检测广播包和多播包 网络畅通的条件:数据包有去有回1.路由器使用路由表找到目标网段 ...
- 数据库缓存mybatis,redis
简介 处理并发问题的重点不在于你的设计是怎样的,而在于你要评估你的并发,并在并发范围内处理.你预估你的并发是多少,然后测试r+m是否支持.缓存的目的是为了应对普通对象数据库的读写限制,依托与nosql ...
- AJAX的创建
20:29:50 创建的步骤: 1.创建异步请求的核心对象 2.设置请求方式和地址 3.设置结果产生的回调函数 4.进行结果的逻辑处理 5.获取结果并处理 6.发送请求 <!DOCTYPE ht ...