题意:
有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. php学习笔记(3)

    1.计数器 <?php /* simple access counter for php3 (c)1998 David W. Bettis dbettis@eyeintegrated.com m ...

  2. linux和windows的文件互传

    Linux →→→Windows 1.使用secureCRT:下载文件只需在shell终端仿真器中输入命令"sz 文件名",即可利用Zmodem将文件下载到本地某目录下. 2. s ...

  3. ID卡

    ID卡全称为身份识别卡(Identification Card),是一种不可写入的感应卡,含固定的编号,主要有台湾SYRIS的EM格式.美国HIDMOTOROLA等各类ID卡.ID卡与磁卡一样,都仅仅 ...

  4. SpringMVC之访问静态文件

    我们在进行springMVC开发时,必定会在jsp页面引入js文件.img文件和css文件.大多数人会将这些分类存放在WebRoot文件下新建的文件夹下面.同时,会在web.xml文件中配置拦截所有请 ...

  5. 试用阿里云RDS的MySQL压缩存储引擎TokuDB

    以前就用过自己搭建MySQL服务器的两种存储引擎MyISAM和InnoDB(也用过一点Memory方式),在今年初转向阿里云关系型数据库服务RDS的时候,看到可调参数中有一个TokuDB,不过不太了解 ...

  6. How to access the properties of an object in Javascript

    Javascript has three different kinds of properties: named data property, named accessor property and ...

  7. js动态加载控件jsp页面

    例子1:(具体参照drp中的flow_card_add.jsp)<script>    var rowIndex = 0;     function addOneLineOnClick() ...

  8. hdu 5007 水 弦

    http://acm.hdu.edu.cn/showproblem.php?pid=5007 纯粹的联系String的substr 什么时候substr拦截比写短话 string te; int n; ...

  9. C#_会员管理系统:开发七(用户分类)

    登录界面(VIPLogin.cs)详细代码: using System; using System.Collections.Generic; using System.ComponentModel; ...

  10. vmware能够ping通内网,上不了外网的解决方法

    一般这是由于里面的路由域名服务器没有配置好. issta@ubuntu:~$ ping www.baidu.com ping: unknown host www.baidu.com 先看一下地址解析器 ...