Codeforces 762B USB vs. PS/2 贪心
题目大意:
有a台只有USB接口的电脑,b台PS/2接口的电脑,c台两种接口都有的电脑。每台电脑只用装一个鼠标。给出n个鼠标及其费用,每个鼠标只能使用一遍。在最大化有鼠标的电脑数目的情况下最小化费用。
\((0 \le a,b,c \leq 10^5 , n \leq 3*10^5 )\)
题解:
这道题我们很容易抽象出来一个网络流模型
我们设超级源汇为S,T,每台电脑和鼠标都抽象成一个节点
我们可以这么建图
S -> 每台电脑 (flow = 1,cost = 0)
USB(PS/2)电脑 -> 所有USB(PS/2)鼠标(flow = 1,cost = 0)
双接口电脑 -> 所有鼠标(flow = 1,cost = 0)
所有鼠标 -> T (flow = 1,cost = \(cost_i\))
然后跑最小费用最大流即可
TLE
我们可以考虑简化构图,我们发现我们每台电脑只有三种连接可能,并且有一种链接方式是由其他的两种链接组成的,所以我们增设两个节点,分别连接所有的USB,PS/2鼠标,然后我们不用从电脑向每个鼠标都连边了,只用加在这两个点上就好了,边数大大减少
TLE
我们发现实际上同种鼠标之间并没有什么区别,所以我么把所有相同的鼠标也缩成一个点.这样把点数降低到了7个点,整张图上只有7个点。但边数还是有些大。。。
TLE
我CNMLGB !!!
我们发现把网络流化简到这个地步以后其实就是一个贪心。。
所以我们模拟这个贪心过程
具体怎么模拟呢。。。
我们把两种不同鼠标分别拍个序,首先取满所有的只能使用这个接口的电脑
然后对于两个接口都能使用的电脑,用一个类似于归并排序的过程即可。
WoC,这么简单的题为什么当时我没想出来!!
T的不要不要的费用流
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 300010;
const int maxnode = 1024;
const int maxedge = maxn<<1;
const int inf = 0x3f3f3f3f;
struct Edge{
int to,next,cap;
ll cost;
}G[maxedge];
int head[maxnode],cnt=1;
inline void add(int u,int v,int c,ll d){
G[++cnt].to = v;
G[cnt].next = head[u];
head[u] = cnt;
G[cnt].cap = c;
G[cnt].cost = d;
}
inline void insert(int u,int v,int c,ll d){
add(u,v,c,d);add(v,u,0,-d);
}
#define v G[i].to
ll dis[maxnode],ans;int flow[maxnode];
const int lim = maxn<<1;
int q[lim + 10],p[maxnode];
int S,T,l,r,flo;bool inq[maxnode];
bool spfa(){
memset(dis,0x3f,sizeof dis);
dis[S] = 0;inq[S] = true;
l = 0;r = -1;q[++r] = S;
flow[S] = 0x3f3f3f3f;
while(l <= r){
int u = q[l % lim];++l;
for(int i = head[u];i;i=G[i].next){
if(dis[v] > dis[u] + G[i].cost && G[i].cap){
dis[v] = dis[u] + G[i].cost;
flow[v] = min(G[i].cap,flow[u]);
p[v] = i;
if(!inq[v]){
inq[v] = true;
q[++r % lim] = v;
}
}
}inq[u] = false;
}if(dis[T] == dis[0]) return false;
ans += 1LL*flow[T]*dis[T];
flo += flow[T];
for(int u = T;u != S;u = G[p[u]^1].to)
G[p[u]].cap -= flow[T],G[p[u]^1].cap += flow[T];
return true;
}
#undef v
char s[10];
int main(){
int a,b,c,n;read(a);read(b);read(c);
read(n);
int nodecnt = n;
S = ++nodecnt;
int node1 = ++nodecnt;insert(S,node1,a,0);
int node2 = ++nodecnt;insert(S,node2,b,0);
int node3 = ++nodecnt;insert(S,node3,c,0);
int mob1 = ++nodecnt;
int mob2 = ++nodecnt;
int mob3 = ++nodecnt;
int x;T = ++nodecnt;
for(int i=1;i<=n;++i){
read(x);scanf("%s",s);
if(!strcmp(s,"USB")){
insert(node1,mob1,inf,0);
insert(node3,mob1,inf,0);
insert(mob1,T,1,x);
}else{
insert(node2,mob2,inf,0);
insert(node3,mob2,inf,0);
insert(mob2,T,1,x);
}
}
while(spfa());
printf("%d %I64d\n",flo,ans);
getchar();getchar();
return 0;
}
正解的贪心
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
template<typename T>inline void read(T &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 300010;
ll c1[maxn],cnt1;
ll c2[maxn],cnt2;
char s[10];
int main(){
int a,b,c,n;read(a);read(b);read(c);
read(n);ll x;
for(int i=1;i<=n;++i){
read(x);
scanf("%s",s);
if(!strcmp(s,"USB")){
c1[++cnt1] = x;
}else{
c2[++cnt2] = x;
}
}
sort(c1+1,c1+cnt1+1);
sort(c2+1,c2+cnt2+1);
int p1=0,p2=0;
ll ans = 0;
for(p1 = 1;p1 <= a && p1 <= cnt1; ++ p1) ans += c1[p1];
for(p2 = 1;p2 <= b && p2 <= cnt2; ++ p2) ans += c2[p2];
c1[cnt1+1] = c2[cnt2+1] = 1LL<<60;
while(c){
if(p1 > cnt1 && p2 > cnt2) break;
if(c1[p1] < c2[p2]) ans += c1[p1++];
else ans += c2[p2++];
--c;
}
printf("%d %I64d\n",p1+p2-2,ans);
getchar();getchar();
return 0;
}
Codeforces 762B USB vs. PS/2 贪心的更多相关文章
- CodeForce-762B USB vs. PS/2(贪心)
USB vs. PS/2 CodeForces - 762B 题意:有三种电脑,分别有a.b.c个,第一种只有USB接口,第二种只有PS/2接口,第三种有两种接口,有m个鼠标,告诉你价钱和接口类型,问 ...
- 【codeforces 762B】USB vs. PS/2
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...
- Codeforces Testing Round #12 B. Restaurant 贪心
B. Restaurant Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/problem ...
- Codeforces 437D The Child and Zoo(贪心+并查集)
题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...
- CodeForces - 777B Game of Credit Cards 贪心
题目链接: http://codeforces.com/problemset/problem/777/B 题目大意: A, B玩游戏,每人一串数字,数字不大于1000,要求每人从第一位开始报出数字,并 ...
随机推荐
- openssl生成https证书
openssl生成https证书 分类: 其它2009-09-03 16:20 452人阅读 评论(0) 收藏 举报 includemoduleaccessapachessl服务器 openssl生成 ...
- java 获取微信 页面授权 获取用户openid
先调用微信的地址 跳转https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx4b4009c4fce00e0c&redirect ...
- Selenium学习(三)Selenium总是崩溃的解决办法
在使用selenium打开浏览器总是崩溃,最近查资料获得可行的解决办法: import sys from selenium import webdriver p = __import__('selen ...
- emacs 简记
简介 Emacs作为神的编辑器,不用介绍了吧,说点感受. 用了一段时间了,总体感觉其实Emacs是很简单的,甚至比vim还简单,因为在X环境下,打开后可以就像记事本一样使用.但是,使用Emacs的人一 ...
- mysql分组查询n条记录
当业务逻辑越来越复杂时,这个查询的需求会越来越多,今天写成笔记记录下来,防止再忘记 SELECT * FROM `notice` a where add_time > 1513008000 an ...
- Android-彻底地理解Binder
转自:https://blog.csdn.net/huachao1001 https://blog.csdn.net/huachao1001/article/details/51504469 你是不是 ...
- mac svn命令行使用入门
本文转载至 http://blog.sina.com.cn/s/blog_6bfa2fc10101euf6.html mac svn命令行使用入门 1. 初始化项目 svn import /Use ...
- 【BZOJ3997】[TJOI2015]组合数学 最长反链
[BZOJ3997][TJOI2015]组合数学 Description 给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走.问至少走多少次才能将财宝捡完.此对此问题变形,假设每个格 ...
- git查看某一次commit里面的内容,即本次commit相对于原来的版本进行了哪些修改
1 知道commit id的话 git show commit-id 2 想要查看某次commit的某个文件进行了哪些修改 git show commit-id filename
- 蓝牙 CTS 测试
安装蓝牙测试安装包 之后 . 安卓包名字 android-cts-6.0_r19-linux_x86-x86.zip 解压之后 /cts/android-cts/tools/ 目录下 运行 ./ ...