Absolute Game

Absolute Game

Alice and Bob are playing a game. Alice has an array aa of n integers, Bob has an array bb 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) — the number of values in each array.

The second line contains n space-separated integers a1,a2,…,an (1≤ai≤10^9) — the numbers in Alice’s array.

The third line contains nn space-separated integers b1,b2,…,bn (1≤bi≤10^9) — the numbers in Bob’s array.

Output
Print the absolute difference between x and y if both players are playing optimally.

Examples
input
4
2 14 7 14
5 10 9 22
output
4
input
1
14
42
output
28

Note
In the first example, the x=14 and y=10. Therefore, the difference between these two values is 4.

In the second example, the size of the arrays is already 1. Therefore, x=14 and y=42.

思路

这道题就很明显博弈论的题目,肯定不会去模拟的,就从小开始分析,如果有都有两个数字,那么后手选的时候,一定会让先手剩下的一个数字取距离两个数字最近的值。好,然后对于先手剩下的最后一个数字,无论剩下哪一个,后手的一定让它剩下距离最近的值,所以对于先手而言要取最大的值,只能从所有数字中选出距下面数组最大的最近距离。然后就过了。。。。。这种题目要从小到大,自己拿手推一推。。。。

#include
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
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=1e9;
		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;
}

你可能感兴趣的:(CF整理题)