Problem Description
We have a deck consisting of N cards. Each card has an integer written on it. The integer on the i-th card from the top is ai.
Two people X and Y will play a game using this deck. Initially, X has a card with Z written on it in his hand, and Y has a card with W written on it in his hand. Then, starting from X, they will alternately perform the following action:
Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.
The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?
Constraints
- All input values are integers.
- 1≤N≤2000
- 1≤Z,W,ai≤109
Input
Input is given from Standard Input in the following format:
N Z W
a1 a2 … aNOutput
Print the score.
Example
Sample Input 1
3 100 100
10 1000 100Sample Output 1
900
If X draws two cards first, Y will draw the last card, and the score will be |1000−100|=900.Sample Input 2
3 100 1000
10 100 100Sample Output 2
900
If X draws all the cards first, the score will be |1000−100|=900.Sample Input 3
5 1 1
1 1 1 1 1Sample Output 3
0
Sample Input 4
1 1 1
1000000000Sample Output 4
999999999
题意:开始时两人手上分半有 z、w 两张牌,有 n 张牌再桌面上,每轮取任意数目的牌,将手上的牌替换成取得牌最下面的那张,然后将剩余的牌扔掉,双方轮流操作直到 n 张牌用完,最后的结果是两人手上的牌的差值 k,先手令 k 尽量大,后手令 k 尽量少,求最后的结果
思路:
由于先手要最大化差值,因此开局他可以取 a[n-1] 剩下 a[n] 或者直接取 a[n] 直接结束游戏,如果先手不这样做,他取得的点数必须大于等于 a[n-1],否则后手会直接拿掉 a[n],但这样先手违背了他最大化差值的任务
同理,后手取小于等于 a[n-1] 的牌,否则会违背他最小化差值的任务,因此最终一定是分别拿到 a[n] 与 a[n-1],故答案是 max(a[n]-a[n-1],a[n]-w)
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
int a[N];
int main() {
int n,z,w;
scanf("%d%d%d",&n,&z,&w);
int maxx=-INF,minn=INF;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int res=0;
if(n==1)
res=abs(a[n]-w);
else
res=max(abs(a[n-1]-a[n]),abs(a[n]-w));
printf("%d\n",res);
return 0;
}