ZOJ - 2968 Difference Game 思维

题目链接:点击查看

Now you are going to play an interesting game. In this game, you are given two groups of distinct integers and C coins. The two groups, named Ga and Gbrespectively, are not empty and contain the same number of integers at first. Each time you can move one integer in one group to the other group. But a move that makes a group empty is considered as invalid. Each move will cost you p coins, and pequals the difference of the size between the two group (take the absolute value. e.g. moving a number from the group of size 4 to the group of size 6 will cost you |4 - 6| = 2 coins, and moving a number between two group with same size will not cost any coins). You can do as many moves as you wish, so long as the total cost does not exceed C.

Let M be the minimum integer in Ga, and N be the maximum integer in Gb. The purpose of this game is to produce a maximum (M - N).

Input

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 60) which is the number of test cases. And it will be followed by T consecutive test cases.

Each case begin with two integers S (1 <= S <= 20000, indicating the size of Ga and Gb at first) and C (0 <= C <= 1000000). Two lines of S integers follow, representing the numbers in Ga and Gb respectively. All these integers are distinct and are between 1 and 50000, both inclusive.

Output

Results should be directed to standard output. The output of each test case should be a single integer in one line, which is the maximum possible value for M - N after some moves.

Sample Input

 

2
1 10
10
12
2 10
1 2
3 4

 

Sample Output

 

-2
1

题意:两个容器原本都有s个数,经过一系列操作,使 GAmin - GBmax 最大,每次移动一个数,花费两个容器个数的差值,总花费不超过c

题解:当然我们是要把GA小的数移到GB,GB大的数移到GA,最好的情况就是使GA的最小值大于GB的最大值,那么我们就枚举每一种情况,判断这种情况的花费是否符合,怎么保证花费最小呢,就是先交替移动,若最后B容器多,那只能在A移向B,反之亦然。若不存在GAmin>GBmax,那就说明最后的GAmin一定就是GA的数了,GBmax是GB的数,为什么呢,因为若GA最后的最小值是GB移动过去的,那也就满足GA的最小值大于GB的最大值了,因此这种情况GA和GB分别先移动c/2次,最后一次取个较大的值即可,当时想的时候大于0的只考虑了最优的情况,没有考虑别的,还是菜啊

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e4+10;
int ga[N],gb[N],gc[N];
int s,c;
map<int,int>mp;
int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		mp.clear();
		scanf("%d%d",&s,&c);
		for(int i=1;i<=s;i++)scanf("%d",&ga[i]), gc[i]=ga[i],  mp[ga[i]]=1;
		for(int i=1;i<=s;i++)scanf("%d",&gb[i]), gc[i+s]=gb[i],mp[gb[i]]=2;
		if(s==1)
		{
			printf("%d\n",ga[1]-gb[1]);
			continue;
		} 
		sort(ga+1,ga+1+s);
		sort(gb+1,gb+1+s);
		sort(gc+1,gc+1+s*2);
		int ans=-1,s1=0,s2,cmax,cmin;
		for(int i=1;i<s*2;i++)
		{
			if(mp[gc[i]]==1) s1++;
			s2=s+s1-i;
			cmax=max(s1,s2);
			cmin=min(s1,s2);
			if((ll)(cmax-cmin)*(cmax-cmin-1)+cmin*2<=c)
			{
				ans=max(ans,gc[i+1]-gc[i]);
			}
		}
		if(ans==-1)
		{
			c=c/2+1;
			ans=max(ga[c]-gb[s-c],ga[c+1]-gb[s-c+1]);
		}
		printf("%d\n",ans);
	}
	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值