D - ABS
Time limit : 2sec / Memory limit : 256MB
Score : 500 points
Problem Statement
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 … aN
Output
Print the score.
Sample Input 1
3 100 100 10 1000 100
Sample 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 100
Sample 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 1
Sample Output 3
0
Sample Input 4
1 1 1 1000000000
Sample Output 4
999999999
思路:答案是max(a[n]-a[n-1], a[n]-w),这种题的思路我一直都比较模糊,记下我目前的想法,因为先手要最大化差值,开局他可以取a[n-1]剩下a[n],或者取a[n]直接结束游戏,这两种情况是先手100%能达到的。如果先手不这样做,他取得牌点数必须>=a[n-1],否则后手会直接拿掉a[n],这样先手就违背他的最大化差值的使命了。然后同理后手就取<=a[n-1]的牌,否则将违背他的使命,这样最终肯定是分别拿到a[n]和a[n-1]的。
# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL a[2003];
int main()
{
int n;
LL z, w;
scanf("%d%lld%lld",&n,&z,&w);
for(int i=1; i<=n; ++i) scanf("%d",&a[i]);
if(n == 1) printf("%lld\n",llabs(a[1]-w));
else
printf("%lld\n",max(llabs(a[n-1]-a[n]), llabs(a[n]-w)));
return 0;
}