http://codeforces.com/problemset/problem/594/A

A. 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 000, n 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.

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

题意:两个人玩游戏,开始有n(偶数)个位置,两人轮流禁用位置,先禁用的人想让最后两个位置距离最小,后禁用的想最大。给你数组a[]表示位置。求禁用完后的距离。

题解:第一个人会禁用最小或者最大的位置,来减少最大距离,第二给人会禁用最中间的数来增加最小距离,所以答案是a[i+n/2]-a[i]其中一个的,由于想要距离最少的人先bans,所以答案为a[i+n/2]-a[i]中的最小值

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<vector> 
 5 #include<fstream> 
 6 #include<algorithm> 
 7 #include<cstring>
 8 #include<iomanip>
 9 using namespace std; 
10 const int maxn=2e5+5; 
11 int a[maxn],n;
12 int main()
13 {
14     scanf("%d",&n);
15     for(int i=1;i<=n;i++)
16     {
17         scanf("%d",&a[i]); 
18     }
19     sort(a+1,a+1+n); 
20     int ans=1e9+5; 
21     for(int i=1;i+n/2<=n;i++)ans=min(ans,a[i+n/2]-a[i]); 
22     printf("%d\n",ans); 
23 } 
View Code

 

转载于:https://www.cnblogs.com/lhclqslove/p/7391017.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值