题意:
有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 【贪心 + 二分】的更多相关文章

  1. poj 2782 Bin Packing (贪心+二分)

    F - 贪心+ 二分 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Description ...

  2. Card Game Cheater(贪心+二分匹配)

    Card Game Cheater Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  3. 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 = { ...

  4. 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

    题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...

  5. 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 ...

  6. 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$ ...

  7. LightOj1383 - Underwater Snipers(贪心 + 二分)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1383 题意:在平面图中,有一条河,用直线y=k表示,河上面(y>k)的都是敌方区 ...

  8. 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/ ...

  9. UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)

      Copying Books  Before the invention of book-printing, it was very hard to make a copy of a book. A ...

随机推荐

  1. FOREIGN KEY相关

    在添加外键的时候可以在最后通过ON指定行为和三个参数,来表示操作主表数据之后外表的变化 比如若是删除主表之后的变化,就可以 ON DELETE + 三个参数 --删除department表中相关数据行 ...

  2. QTableView另类打印解决方案(复用render函数去解决print问题)

    Qt QTableView另类打印解决方案     上回书说道Qt的model/view,我就做了个demo用于显示数据库中的内容.没想到tableview的打印竟然成了问题.我困惑了,难道Qt不应该 ...

  3. hdoj 1247 Hat’s Words(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 思路分析:题目要求找出在输入字符串中的满足要求(该字符串由输入的字符串中的两个字符串拼接而成)的 ...

  4. webservice的讲解

    Web Service实践之——开始XFire 一.Axis与XFire的比较 XFire是与Axis2 并列的新一代WebService平台.之所以并称为新一代,因为它: 1.支持一系列Web Se ...

  5. break 与continue的区别

    //break是结束整个循环体,continue是结束单次循环 比方说: while(x++ < 10){ if(x == 3) { break; } printf("%d\r\n&q ...

  6. mysql三个应用场景

    场景一,数据表自动备份(多个数据表字段同步等),使用触发器.如updatelog记录对资源的所有操作日志,reslastlog记录资源最后操作的日志信息.同步方式实现如下: //创建表 DROP TA ...

  7. java程序获得SqlServer数据表的表结构

    /**   * 取得一张表的结构信息   * 使用DatabaseMetaData与ResultSetMetaData结合的方式获取全部属性   * @param conn   数据连接   * @p ...

  8. javadoc入门

    斌斌 (给我写信) 原创博文(http://blog.csdn.net/binbinxyz),转载请注明出处! java凝视 java里面有两种类型的凝视.一种是以"/*"起头,以 ...

  9. Android API 中文(77)——AdapterView.OnItemSelectedListener

    前言 本章内容是android.widget.AdapterView.OnItemSelectedListener,版本为Android 2.3 r1,翻译来自"cnmahj",欢 ...

  10. Request url 各种属性值

    網址:http://localhost:1897/News/Press/Content.aspx/123?id=1#toc Request.ApplicationPath / Request.Phys ...