ZOJ 2968 Difference Game 【贪心 + 二分】
题意:
有Ga、Gb两堆数字,初始时两堆数量相同。从一一堆中移一一个数字到另一一堆的花费定义为两堆之间数
量差的绝对值,初始时共有钱C。求移动后Ga的最小小值减Gb的最大大值可能的最大大值。
思路:
假如有足足够钱移动,那么Ga的最大大值和Gb的最小小值应该是两堆合并后排序中相邻的两数。那么我们
就枚举这个数。枚举的时候我们需要确定形成这个情况(大大的都在Ga堆,小小的都在Gb堆)的最少花
费,这个花费可以这样解决,假设Ga中向Gb中需要移入入ab个数,Gb需要向Ga移入入ba个数,首首先当
然是Ga移一一个给Gb,然后Gb移一一个给Ga,这样直到某一一堆需要移出都移出停止止,此时的花费就是
2*min(ab,ba)。接着就是其中一一堆一一直移给另一一堆,每移一一次费用用会增2,所以此时的花费为
|ab-ba|*(|ab-ba|-1)。总花费就为2*min(ab,ba) + |ab - ba| *(|ab -ba|- 1)。
当然可能没有足足够钱移动,Ga堆的最小小值永远小小于Gb堆的最大大值,这样我们当然要把Ga堆前C/2
(C/2+1)小小的移到Gb堆,Gb堆前C/2+1(C/2)的移到Ga堆,当然是交替移。 By @sake
Source Code:
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0) using namespace std; int ga[], gb[], gc[];
bool cmp(const int & a, const int &b){
return a > b;
}
//二分查找,关键字key,右边界n,左边界默认-1
int b_search(int key, int n){
int l = -, r = n, m;
while (l+ < r) {
m = (l+r)/;
if (ga[m] < key) {
l = m;
} else {
r = m;
}
}
return r;
}
int main(){
int t;
scanf("%d", &t);while (t--) {
int n, cost;
scanf("%d%d", &n, &cost);
for (int i = ; i < n; i ++) {
scanf("%d", &ga[i]);
gc[i] = ga[i];
}
for (int i = ; i < n; i ++) {
scanf("%d", &gb[i]);
gc[i+n] = gb[i];
}
if (n == ) {
printf("%d\n", ga[] - gb[]);
continue;
}
sort(ga, ga+n);
sort(gc, gc+*n);
sort(gb, gb+n, cmp);
int maxn = -;
for (int i = ; i < *n; i ++) {
int ab = b_search(gc[i], n);
int ba = (*n - i) - (n - ab);
int mab = min(ab, ba);
int fab = abs(ab-ba);
int c = *mab + (fab-)*fab;
if (c <= cost) {
maxn = max(maxn, gc[i]-gc[i-]);
}
}
if (maxn > ) {
printf("%d\n", maxn);
} else {
int ans = max(ga[cost/]-gb[cost/+], ga[cost/+]-gb[cost/]);
printf("%d\n", ans);
}
}
return ;
}
ZOJ 2968 Difference Game 【贪心 + 二分】的更多相关文章
- poj 2782 Bin Packing (贪心+二分)
F - 贪心+ 二分 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description ...
- Card Game Cheater(贪心+二分匹配)
Card Game Cheater Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - F 贪心+二分
Heap Partition Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A sequence S = { ...
- 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II
题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...
- Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找
The link to problem:Problem - D - Codeforces D. Range and Partition time limit per test: 2 second ...
- ZOJ 4062 - Plants vs. Zombies - [二分+贪心][2018 ACM-ICPC Asia Qingdao Regional Problem E]
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4062 题意: 现在在一条 $x$ 轴上玩植物大战僵尸,有 $n$ ...
- LightOj1383 - Underwater Snipers(贪心 + 二分)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1383 题意:在平面图中,有一条河,用直线y=k表示,河上面(y>k)的都是敌方区 ...
- Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 贪心/二分
C. GukiZ hates Boxes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...
- UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)
Copying Books Before the invention of book-printing, it was very hard to make a copy of a book. A ...
随机推荐
- PhpStorm 10.0.1安装(win7-64位)
软件下载地址 http://www.xiazaiba.com/html/25261.html#j_down_list 或者:http://pan.baidu.com/s/1brSA9C 密码:tpc7 ...
- zoj 3195 Design the city lca倍增
题目链接 给一棵树, m个询问, 每个询问给出3个点, 求这三个点之间的最短距离. 其实就是两两之间的最短距离加起来除2. 倍增的lca模板 #include <iostream> #in ...
- Python之路Day2
-->the start 养成好习惯,每次上课的内容都要写好笔记. 第二天内容主要是熟悉int.long.float.str.list.dict.tuple这几个类的内建方法. 对于Python ...
- IOS 学习笔记(1) 视图UIViewController
1.UIViewController *newController=[[UIViewController alloc] initWithNibName:@"XXX" bundle: ...
- 分组求和SQL示例
1.ROLLUP和CUBE函数,自动汇总数据 select * from test_tbl的数据这样的 col_a col_b col_c ---- ----- ...
- ID卡
ID卡全称为身份识别卡(Identification Card),是一种不可写入的感应卡,含固定的编号,主要有台湾SYRIS的EM格式.美国HIDMOTOROLA等各类ID卡.ID卡与磁卡一样,都仅仅 ...
- 手机SIM卡介绍 三类不同标准的SIM卡
SIM卡的全称是Subscriber Identity Module,翻译过来也叫客户识别模块,也叫做智能卡.用户身份识别卡.这块小小的芯片可以存储用户的号码.信息,以及一定数量的联系人数据,配合我们 ...
- hdoj 1532 Drainage Ditches(最大网络流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路分析:问题为最大网络流问题,给定一个有向图,需要求解该有向图的最大网络流,使用Edmonds ...
- Apache 错误代码配置
ErrorDocument 400 /error_pages/400.htmlErrorDocument 401 /error_pages/401.htmlErrorDocument 403 /err ...
- Just another Robbery(背包)
1079 - Just another Robbery PDF (English) Statistics Forum Time Limit: 4 second(s) Memory Limit: 3 ...