ICPC Southeastern Europe Contest 2019 H. Absolute Game

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.

输出时每行末尾的多余空格,不影响答案正确性

样例输入1

4
2 14 7 14
5 10 9 22

样例输出1

4

样例输入2

1
14
42

样例输出2

28

题解:

可以知道A想要差值最大,而B要差值最小,那么A每次拿去的肯定是拿去最大的或者最小的,也就是与B的数据差的最近的,而B为最后一手,那么他最后一个拿走的肯定是与离得最远的那个数;

因为数据不大,暴力模拟,把所有的情况遍历一遍,取最大的最小值即可;

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f
using namespace std;

typedef long long ll;
const int mod = 1e9 + 7;
const ll maxn = 10005;
const double pi = acos(-1.0);

int n;
int a[maxn];
int b[maxn];

int main()
{
    while(cin >> n)
    {
        for(int i = 0; i < n; i ++)
            cin >> a[i];
        for(int i = 0; i < n; i ++)
            cin >> b[i];
        int ans = -1;
        for(int i = 0; i < n; i ++)
        {
            int num = INF;
            for(int j = 0; j < n; j ++)
            {
                num = min(num, abs(a[i] - b[j]));
            }
            ans = max(ans, num);
        }
        cout << ans << endl;
    }
    return 0;
}

你可能感兴趣的:(做题记录)