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<iostream>
#include<iomanip>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#include<stack>
#include<queue>
#include<sstream>
#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值