Codeforces 595C-Warrior and Archer(思维博弈)
作者:互联网
题目链接:https://codeforces.com/problemset/problem/595/C
博客园食用链接: https://www.cnblogs.com/lonely-wind-/p/13444856.html
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 x 1 , x 2 , . . . , x n x_1, x_2, ..., x_n 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
x
1
,
x
2
,
.
.
.
,
x
n
(
0
≤
x
i
≤
1
0
9
)
x_1, x_2, ..., x_n (0 ≤ x_i ≤ 10^9)
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个点在x轴上,Vova每次删除一个点,Lesha每次删除一个点,最后使得只剩下两个点(n保证是个偶数),Vova先手。其中Vova想使得最后的两个点的距离尽可能地小,Lesha想使得最后两个点的距离尽可能地大。问最后剩下两个点的距离是多少?
我们先来手动模拟一下样例,Vova要使得最后的距离最小,那么他肯定会看中 0 − 1 0-1 0−1这个距离,而Lesha要使得距离最大,他肯定会看中 0 − 31 0-31 0−31这个距离。那么Vova先手的时候一定会删去 31 31 31这个点,之后Lesha会删去 1 1 1这个点,然后Vova删 15 15 15,Lesha删 3 3 3。那么最后就剩下了 0 − 7 0-7 0−7
然后通过上面的模拟我们会发现,一旦一个起点 p p p确定了,那么这个起点是不会被改变的!而和他距离最近的 n 2 − 1 \frac{n}{2}-1 2n−1个点一定不会存在,那么似乎答案也就固定了!那么我们从1枚举过去,答案就是 x n 2 + p − x p x_{\frac{n}{2}+p}-x_p x2n+p−xp的最小值。
以下是AC代码:(难以想象2300分的题就这么两行)
#include <bits/stdc++.h>
using namespace std;
const int mac=2e5+10;
const int inf=1e9+10;
int a[mac];
int main(int argc, char const *argv[])
{
int n;
scanf ("%d",&n);
for (int i=1; i<=n; i++) scanf ("%d",&a[i]);
sort(a+1,a+1+n);
int ans=inf;
for (int i=1; i<=n/2; i++){
ans=min(ans,a[i+n/2]-a[i]);
}
printf("%d\n",ans);
return 0;
}
标签:Warrior,int,positions,31,Lesha,Codeforces,595C,Vova,position 来源: https://blog.51cto.com/u_15249461/2870373