bzoj 3969 WF2013 Low Power [贪心] [二分答案]
来源:互联网 发布:苹果网络不可用 编辑:程序博客网 时间:2024/05/29 18:50
3969: [WF2013]Low Power
Time Limit: 10 Sec Memory Limit: 256 MB
Submit: 365 Solved: 171
Description
有n个机器,每个机器有2个芯片,每个芯片可以放k个电池。
每个芯片能量是k个电池的能量的最小值。
两个芯片的能量之差越小,这个机器就工作的越好。
现在有2nk个电池,已知它们的能量,我们要把它们放在n个机器上的芯片上,
使得所有机器的能量之差的最大值最小。
Input
第一行,两个正整数,n和k。
第二行,2nk个整数,表示每个电池的能量。
Output
一行一个整数,表示所有机器的能量之差的最大值最小是多少。
Sample Input
2 3
1 2 3 4 5 6 7 8 9 10 11 12
Sample Output
1
HINT
2nk <= 10^6, 1 <= pi <= 10^9。
也就是说有n*k*2个电池放进每个机器两个电池组,使得每个机器每个电池组的最小值之差的最大值最小。
那么二分+check即可。
最小的一定是排序后相邻两个值,因为如果不是这两个的话,答案不会更优。
可以不用直接模拟,只需要找到当前的电池位置之后看剩下的电池数量能不能满足剩下的cnt个机器。
#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;#define smax(x,tmp) x=max((x),(tmp))#define smin(x,tmp) x=min((x),(tmp))#define maxx(x1,x2,x3) max(max(x1,x2),x3)#define minn(x1,x2,x3) min(min(x1,x2),x3)const int INF=0x3f3f3f3f;const int maxn = 1000005;int n,k;int battery[maxn];/*bool used[maxn];struct Node{ int cur[2],cnt;}node[maxn];inline bool check(int lim){ memset(node,0,sizeof(node)); memset(used,0,sizeof(used)); int pos = 0; for(int i=1;i<=(n*k<<1);i++) if(battery[i+1]-battery[i]<=lim) { Node &t = node[++pos]; t.cur[0] = battery[i]; t.cur[1] = battery[i+1]; t.cnt = 2; used[i] = used[i+1] = true; i++; if(pos==n) break; } if(pos^n) return false; pos = 1; // set for batteries for(int i=1;i<=n;i++) // set for the machines while(node[i].cnt ^ k) { int id = 0; if(node[i].cur[1] < node[i].cur[0]) id ^= 1; while(pos<=(n*k<<1) && used[pos]) pos++; if(pos == (n*k<<1) + 1) return false; // lack of batteries if(node[i].cur[id] > battery[pos]) return false; // non profiter pas plus node[i].cur[id] = battery[pos]; node[i].cnt++; pos++; } return true;}*/inline bool check(int lim){ int cnt = n; for(int i=1;cnt && i<=(n*k<<1);i++) // si les tout machine sur battery, break if(i^(n*k<<1) && battery[i+1]-battery[i]<=lim) { if((n*k<<1)-i+1 < (cnt*k<<1)) return false; // C'est no necessaire par la comparison en array cnt--; i++; } return !cnt; // si non de profiter}int work(){ int l = battery[2] - battery[1] , r = battery[n*k<<1]; while(l<r) { int m = (l+r)>>1; bool tmp = check(m); if(tmp) r = m; else l = m + 1; } return l;}int main(){#ifndef ONLINE_JUDGE freopen("battery.in","r",stdin); freopen("battery.out","w",stdout);#endif scanf("%d%d",&n,&k); for(int i=1;i<=(n*k<<1);i++) scanf("%d",battery+i); sort(battery+1,battery+(n*k<<1)+1); int ans = work(); printf("%d",ans); return 0;}
0 0
- bzoj 3969 WF2013 Low Power [贪心] [二分答案]
- [BZOJ3969]WF2013 Low Power|二分答案|贪心
- bzoj 3969: [WF2013]Low Power
- BZOJ 3969: [WF2013]Low Power
- BZOJ3969 [WF2013]Low Power
- Kattis-Low Power【二分】
- [二分答案 贪心] BZOJ 3248 [ioi2013]robots
- BZOJ 2097 Exercise 奶牛健美操 二分答案+树形DP+贪心
- BZOJ 2525 Poi2011 Dynamite 二分答案+树形贪心
- BZOJ 2067 POI 2004 SZN 树形DP 贪心 二分答案
- [贪心 模拟 + 二分答案 DP] BZOJ 1181 [CROATIAN2009]IZBROI选举
- bzoj 2525: [Poi2011]Dynamite 二分答案+树形贪心
- bzoj 3613: [Heoi2014]南园满地堆轻絮 二分答案+贪心
- 【jzoj4932】【贪心】【二分答案】
- 数学题 贪心+二分答案
- poj3122 二分答案+贪心
- bzoj 2016 二分答案
- BZOJ-5090 (二分答案)
- base64编解码
- 中间件随笔2-ZooKeeper概述
- spring task 定时任务 注解方式 demo
- 勾股定理一日一证连载130
- VS无法打开项目文件“Web.csproj” -此安装不支持该项目类型问题解决方案
- bzoj 3969 WF2013 Low Power [贪心] [二分答案]
- Java String类、StringBuffer和StringBuilder类
- spring前端控制器映射路径
- 你是不是得了拖延症?
- 基于地图的短信追踪小练习(3)短信的接收与发送
- php中删除和判断变量
- MD5加解密
- 打不死的redis集群
- KVO使用