H. Absolute Game
Alice and Bob are playing a game. Alice has an array a of n integers, Bob has an array b of n 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)(1≤n≤1000) — the number of values in each array.
The second line contains n space-separated integers a_1,a_2,…,a_n (1≤a_i≤10^9)a1,a2,…,a**n(1≤a**i≤109) — the numbers in Alice’s array.
The third line contains n space-separated integers b_1,b_2,…,b_n (1≤b_i≤10^9)b1,b2,…,b**n(1≤b**i≤109) — the numbers in Bob’s array.
Output
Print the absolute difference between x and y if both players are playing optimally.
输出时每行末尾的多余空格,不影响答案正确性
4
2 14 7 14
5 10 9 22
4
1
14
42
28
题解:
可以知道A想要差值最大,而B要差值最小,那么A每次拿去的肯定是拿去最大的或者最小的,也就是与B的数据差的最近的,而B为最后一手,那么他最后一个拿走的肯定是与离得最远的那个数;
因为数据不大,暴力模拟,把所有的情况遍历一遍,取最大的最小值即可;
#include
#include
#include
#include
#include
#include
#include