G - Warrior and Archer CodeForces - 595C(博弈)

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, 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 ≤ 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:

Vova bans the position at coordinate 15;
Lesha bans the position at coordinate 3;
Vova bans the position at coordinate 31;
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、B轮流删点,最后剩下两个点。A想让剩下两个点尽量近,B想让剩下两个点尽量远,A,B均为最优策略,求最后两点距离。

思路:
每次写博弈都能学到新的神奇姿势。

假设最后剩下两个点为 l , r l,r ,则A想让点接近,只会删除 [ l , r ] [l,r] 外的点。
B想让点远离,只会删除 [ l , r ] [l,r] 内的点。

所以一定满足 r l = n / 2 r-l=n/2

但是有很多对 l , r l,r 怎么办呢?

因为A是先手,所以假设有 [ l 1 , r 1 ] [l1,r1] [ l 2 , r 2 ] [l2,r2] ,两个点对都满足序号距离为n/2。
a [ r 1 ] a [ l 1 ] > a [ r 2 ] a [ l 2 ] a[r1]-a[l1]>a[r2]-a[l2] ,那么A可以把 [ l 1 , r 1 ] [l1,r1] 给破坏掉,所以我们只要找满足 r l = n / 2 r-l=n/2 的最小距离 ( l , r ) (l,r) 。也就是,先手肯定更占优势。

感觉证明不是很严谨,没办法,这就是“思维题”吧。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;

int a[maxn];

int main() {
    int n;scanf("%d",&n);
    for(int i = 1;i <= n;i++) {
        scanf("%d",&a[i]);
    }
    sort(a + 1,a + 1 + n);
    int ans = 2e9;
    for(int i = n / 2 + 1;i <= n;i++) {
        ans = min(ans,a[i] - a[i - n / 2]);
    }
    printf("%d\n",ans);
    return 0;

}

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/107871259
今日推荐