Codeforces 594A - Warrior and Archer (思维)

看官方题解这么长,本来想过几天看其他巨巨写的题解的。然而到了今天也没人写TAT。

## 题意

Archer和Warrior在PK,有n个位置可以选。每轮他们轮流禁用一个位置,到最后剩下两个位置。Archer希望距离尽量远,Warrior希望距离尽量近。问最后距离是多少。

## 思路

我们假设最后留下来的位置是l和r。

接下来我们要证明l和r之间的距离一定是n/2 - 1!

首先我们证明距离不会大于n/2 - 1。

假设距离 > n/2 - 1,那么显然Warrior肯定在其中选了一个位置。但是如果Warrior这么做,他完全可以放弃这个位置,去选L或者R,这样就能缩短最终距离。所以这种情况不可能。 既然如此,那么距离肯定是 <= n/2 - 1。Archer只能努力让距离保持在n/2 - 1。 Warrior的最优选择肯定是选择最左端或者最右端的空,Archer总是选 剩下能选择的空里的 中间那个空,所以到最后[L, R]之间Archer肯定能选择n/2 - 1步,也就是距离肯定是 n/2 - 1。

所以最终R - L = n / 2

因为Warrior先手,所以答案是arr[R] - arr[L]的最小值。

#include <stack>
#include <cstdio>
#include <list>
#include <cassert>
#include <set>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <string>
#include <map>
#include <cmath>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(p, num) memset(p, num, sizeof(p))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define FOR(i, a, b) for (int i=(a); (i) < (b); (i)++)
#define FOOR(i, a, b) for (int i = (a); (i)<=(b); (i)++)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 2e5+10;
const int MOD = 1e9 + 7;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int seed = 131;
int cases = 0;
typedef std::pair<int, int> pii;
 
int arr[MAXN];
 
int main()
{
   // ROP;
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
    sort(arr, arr+n);
    int ans = INF;
    for (int i = 0; i + n/2 < n; i++) ans = min(ans, arr[i+n/2] - arr[i]);
    printf("%d\n", ans);
    return 0;
}

Powered by Jekyll and Theme by solid