【Codeforces Round 330 (Div 2)C】【博弈 贪心 脑洞】n个数AB轮流选到只剩俩,A希望差小B希望差大问最后差值

84 篇文章 5 订阅
48 篇文章 0 订阅

Warrior and Archer
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

In the official contest this problem has a different statement, for which jury's solution was working incorrectly, and for this reason it was excluded from the contest. This mistake have been fixed and the current given problem statement and model solution corresponds to what jury wanted it to be during the contest.

Vova and Lesha are friends. They often meet at Vova's place and compete against each other in a computer game named The Ancient Papyri: Swordsink. Vova always chooses a warrior as his fighter and Leshac chooses an archer. After that they should choose initial positions for their characters and start the fight. A warrior is good at melee combat, so Vova will try to make the distance between fighters as small as possible. An archer prefers to keep the enemy at a distance, so Lesha will try to make the initial distance as large as possible.

There are n (n is always even) possible starting positions for characters marked along the Ox axis. The positions are given by their distinct coordinates x1, x2, ..., xn, two characters cannot end up at the same position.

Vova and Lesha take turns banning available positions, Vova moves first. During each turn one of the guys bans exactly one of the remaining positions. Banned positions cannot be used by both Vova and Lesha. They continue to make moves until there are only two possible positions remaining (thus, the total number of moves will be n - 2). After that Vova's character takes the position with the lesser coordinate and Lesha's character takes the position with the bigger coordinate and the guys start fighting.

Vova and Lesha are already tired by the game of choosing positions, as they need to play it before every fight, so they asked you (the developer of the The Ancient Papyri: Swordsink) to write a module that would automatically determine the distance at which the warrior and the archer will start fighting if both Vova and Lesha play optimally.

Input

The first line on the input contains a single integer n (2 ≤ n ≤ 200 000n is even) — the number of positions available initially. The second line contains n distinct integers x1, x2, ..., xn (0 ≤ xi ≤ 109), giving the coordinates of the corresponding positions.

Output

Print the distance between the warrior and the archer at the beginning of the fight, provided that both Vova and Lesha play optimally.

Sample test(s)
input
6
0 1 3 7 15 31
output
7
input
2
73 37
output
36
Note

In the first sample one of the optimum behavior of the players looks like that:

  1. Vova bans the position at coordinate 15;
  2. Lesha bans the position at coordinate 3;
  3. Vova bans the position at coordinate 31;
  4. Lesha bans the position at coordinate 1.

After these actions only positions 0 and 7 will remain, and the distance between them is equal to 7.

In the second sample there are only two possible positions, so there will be no bans.



#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=2e5+10,M=0,Z=1e9+7,ms63=1061109567;
int n;
int a[N];
int main()
{
	while(~scanf("%d",&n))
	{
		for(int i=1;i<=n;++i)scanf("%d",&a[i]);
		sort(a+1,a+n+1);
		int ans=2e9;
		int dis=n/2;
		for(int i=1;i+dis<=n;++i)gmin(ans,a[i+dis]-a[i]);
		printf("%d\n",ans);
	}
	return 0;
}
/*
【trick&&吐槽】
这题非常地有趣,当时比赛的时候不知道哪里出错误了,竟然没人AC。
虚拟赛的时候成了第二个过题的,233333

【题意】
给你n(2<=n<=2e5且n为偶数)个位置,每个位置的取值范围都在[0,1e9],且所有位置两两不同。
A和B轮流选数,A先手,一共操作(n-2)/2轮次,直到最后只剩下2个数为止。
A希望使得剩下的2个数之差(绝对值)尽可能小,B希望使得剩下的2个数之差(绝对值)尽可能大。
A、B两人都用最优策略。问你,最后剩下的2个数之差(绝对值)是多少。

【类型】
博弈 贪心 脑洞

【分析】
这道题很有趣,并不是传统的博弈。
我们可以按照模拟的思路入手——
首先,A想要使得数尽可能接近,他肯定去除的是最小或最大的数。
这个很显然成立,因为如果对于a[first]或a[last],如果不把这个数移除,那么它最终就是剩下的两个数之一。
也就是说,我们早晚要去除这个边界的数。
这个博弈是没有局部性质的,也就是,移除数字的先后并不重要,我们使得最终的决策集合相同即可。
于是可以认定A一定移除边界的数。
而B呢?B想要使得两个数尽可能远,他肯定去除的是中间的数。

我们换一种说法——
A想要使得两个数尽可能近,而这两个数的下标距离,再小也不能小于n/2。
B想要使得两个数尽可能远,而这两个数的下标距离,再大也不能超过n/2。
于是最后博弈出的两个数的下标距离,就会恰好是n/2
A可以决定的是,最后剩下哪两个这种要求的数。
于是我们暴力扫描一遍,gmin(ans,a[i+dis]-a[i]),这道题就做完啦!

【时间复杂度&&优化】
O(n)

*/


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值