zoj2968 Difference Game
分析:
一 首先答案可以是正数。
那就简单了,先把所有的数进行升序排列,因为ga数组最小的数一定比gb数组最大的数大。所以排列后一定会在相连的位置得到答案。所以就从左到右便利分割点。
左边作为ga数组,右边作为gb数组,然后进行原数组的模拟,然后判断下cost有没有超过c即可。
如果发现不可能为正数,那就必须是负数了。
二 负数的情况
因为每交换一对数会花费2,而且最后还可以进行一次交换(0的时候).所以就简单了,只需要判断一对一对交换后,最后要交换的那个位置,2种情况取(就是上面最小的下来,下面最大的上去)最大的结果就可以了。奇数偶数是一样的做法。
然后,第一步取正数的过程中,因为a数组是有大小的所以可以直接二分位置。因为题目数据量不是很大,为求方便我就直接遍历了。
上代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<stack>
#include<algorithm>
using namespace std;int ga[20005],gb[20005],gc[50000];
int s,c;int Max=-50006;
int flag=0;
void solve1(){int i,j,k;for(i=1;i<2*s;i++){int sum1=0,sum2=0;for(j=s-1;j>=0;j--){if(gc[i]>ga[j]) break;else sum1++;}for(k=0;k<=s-1;k++){if(gc[i]<=gb[k]) break;else sum2++;}int ka=s-sum1,kb=s-sum2;int temp=min(ka,kb);int cost=temp*2;int k=abs(ka-kb);cost+=k*(k-1);if(cost<=c){flag=1;Max=max(Max,gc[i]-gc[i-1]);}}
}int main()
{int t;scanf("%d",&t);while(t--){int i;scanf("%d%d",&s,&c);for(i=0;i<s;i++) {scanf("%d",&ga[i]);gc[i]=ga[i];}for(i=0;i<s;i++){scanf("%d",&gb[i]);gc[i+s]=gb[i];}if(s==1){printf("%d\n",ga[0]-gb[0]);continue;}sort(ga,ga+s);sort(gb,gb+s);sort(gc,gc+2*s);flag=0;Max=-50006;solve1();if(flag==1) printf("%d\n",Max);else{Max=max(Max,ga[c/2+1]-gb[s-c/2-1]);Max=max(Max,ga[c/2]-gb[s-c/2-2]);printf("%d\n",Max);}}
}
zoj2968 Difference Game相关推荐
- ADPRL - 近似动态规划和强化学习 - Note 10 - 蒙特卡洛法和时序差分学习及其实例 (Monte Carlo and Temporal Difference)
Note 10 蒙特卡洛法和时序差分学习 Monte Carlo and Temporal Difference 蒙特卡洛法和时序差分学习 Note 10 蒙特卡洛法和时序差分学习 Monte Car ...
- Linux: what's the difference between /proc/self and /proc/$$?
我在Stack Overflow上提了一个问题:linux - what's the difference between /proc/self and /proc/$$? - Stack Overf ...
- python 集合set 的三大方法intersection union difference来处理文氏图
TODO - 练习:A或B,但不能同时包含 编写一个函数,将两个集合(set_a和set_b)作为输入,并返回一个新的集合,其中包含set_a或set_b中的元素,但不包含两者兼有的元素. 在上面的文 ...
- Difference between prop and attr in different version of jquery
jQuery <1.9 $('#inputId').attr('readonly', true); jQuery 1.9+ $('#inputId').prop('readonly', true ...
- What’s the Difference between a Hue, Tint, Shade and Tone ?
What's the Difference between a Hue, Tint, Shade and Tone ? Ask Yourself first- What's the differenc ...
- G面经prepare: Set Intersection Set Difference
求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4] difference 类似merge, 分小于等于大于三种情况,然后时间O(m+n ...
- LeetCode——Find the Difference
LeetCode--Find the Difference Question Given two strings s and t which consist of only lowercase let ...
- python 并集union, 交集intersection, 差集difference, 对称差集symmetric_difference
python的集合set和其他语言类似,是一个无序不重复元素集, 可用于消除重复元素. 支持union(联合), intersection(交), difference(差)和sysmmetric d ...
- 理解一条语句:SELECT difference(sum(value)) FROM mq_enqueue WHERE channel =~ /ActiveMQ_TEST/ AND $tim...
最近使用grafana在查询InfluxDB中,用到了这一条语句 SELECT difference(sum("value")) FROM "mq_enqueue&quo ...
最新文章
- 攻击面管理预防网络攻击原理?
- 特征工程(二) :文本数据的展开、过滤和分块
- [安卓基础] 006.打开另一个Activity
- 华为服务器系统关机命令,云服务器关机指令
- 从零开始学习docker(十六)Swarm mode 创建集群
- docker 搭建nginx php mysql_Docker搭建PHP/Nginx/MySQL/Composer环境
- ES6 iterator 迭代器
- LeetCode 354. 俄罗斯套娃信封问题(最长上升子序 DP/二分查找)
- Docker环境搭建,K8s
- vue - rimraf
- 拼多多算法工程师笔试题之分配巧克力
- 第二百五十一节,Bootstrap项目实战--响应式轮播图
- 手写个Tomcat雏型
- FREQCON OVERSPEED 1.2 368U4 204S
- 转载:“技术支持工程师”薪资行情
- 怎么直接压缩图片?好用的图片压缩工具推荐
- Led显示屏设备自发现技术实现-上海灵信
- 计算机工程学院文艺例会,西航职院 | 计算机工程学院 2019年度学生会干部第一次例会...
- 计算机授课教案模板,讲课教案模板.doc
- python创建子目录并在子目录下创建文件