G - Absolute Game
Alice and Bob are playing a game. Alice has an array aa of nn integers, Bob has an array bb of nn integers. In each turn, a player removes one element of his array. Players take turns alternately. Alice goes first.
The game ends when both arrays contain exactly one element. Let x be the last element in Alice's array and y be the last element in Bob's array. Alice wants to maximize the absolute difference between x and y while Bob wants to minimize this value. Both players are playing optimally.
Find what will be the final value of the game.
Input
The first line contains a single integer n (1≤n≤1000) — the number of values in each array.
The second line contains nn space-separated integers a1,a2,…,an (1≤ai≤109) — the numbers in Alice's array.
The third line contains nn space-separated integers b1,b2,…,bn (1≤bi≤109) — the numbers in Bob's array.
Output
Print the absolute difference between xx and yy if both players are playing optimally.
Examples
4 2 14 7 14 5 10 9 22
4
1 14 42
28
Note
In the first example, the x=14x=14 and y=10y=10. Therefore, the difference between these two values is 44.
In the second example, the size of the arrays is already 11. Therefore, x=14x=14 and y=42y=42.
題目鏈接:https://vjudge.net/contest/345791#problem/G
試題解析
我們先對a,b兩個數組求最小差值數組c,即ci=min(abs(ai-bj))。
我們知道存在bi使得ai與bi的差值最小,所以每當Alice去掉一個ai時,Bob就去掉與之對應的bi,為什么去掉bi?
當a數組與b數組取最小值是一一對應時,我們知道bi是使ai為最小的所以去掉ai后,bi不會使得a數組中其他數的最小值變得更小,所以Bob此時去掉相應的bi。Alice丟棄一個數時一定是將使得ci值最小的數去掉,到最后我們就會發現我們得到的值就是ci數組的中的最大值。
當a數組與b數組取最小值不是一一對應時,Alice還是會去掉使得ci值最小的那一個ai,當存在aj==ai時就說明會有一個b不會使得任何一個a最小,此時Bob就會去掉這個數b,到最后得到的值還是ci數組中的最大值。
所以我們只需要將ci數組求出來再找到最小值就好了。
#include<bits/stdc++.h> using namespace std; #define inf 0x3f3f3f3f typedef long long ll; const int mx=2e5+10; int s1[mx],s2[mx]; int n; int main() { int ans=-1; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&s1[i]); for(int i=1;i<=n;i++) scanf("%d",&s2[i]); for(int i=1;i<=n;i++) { int m=inf; for(int j=1;j<=n;j++) { m=min(m,abs(s1[i]-s2[j])); } ans=max(ans,m); } printf("%d\n",ans); return 0; }