HUSTOJ:ABS

问题 G: ABS

时间限制: 1 Sec  内存限制: 128 MB
提交: 537  解决: 186
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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 is given from Standard Input in the following format:
N Z W
a1 a2 … aN

输出

Print the score.

样例输入

3 100 100
10 1000 100

样例输出

900

提示

If X draws two cards first, Y will draw the last card, and the score will be |1000−100|=900.


  博弈问题。

  大概题意:

  X,Y两个人,手中各有一张牌,牌上的数字大小分别是Z,W。此时桌子上有N张牌,X、Y两个人一前一后从桌子上拿任意多的牌。

  每次拿到几张牌后,把手头原有的牌扔掉,只保留刚刚拿起的这些牌里最后的一张。

  一直到拿到桌子上没有牌之后,比较X、Y两个人手中牌的差。

  X的目的是让结果最大化,Y的目的是让结果最小化 。

  

  这个题做的时候没有想到什么办法,因为桌子上的牌的数值是不确定的,没有办法根据这个直接判断,所以当时干脆让X先拿的时候拿到第 n-1 个或者第 n 个,然后进行对比。

  哈?竟然过了emmmmm。。

  上代码(已AC)

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<math.h>
 4 using namespace std;
 5 
 6 int main(){
 7     int n, z, w, a[2010];
 8     cin >> n >> z >> w;
 9     for(int i=1;i<=n;i++) cin >> a[i];
10     if(n==1) {
11         cout << llabs(a[n]-w);
12     }
13     else{
14         cout << max(llabs(a[n]- a[n-1]), llabs(a[n] - w));
15     }
16     return 0; 
17 }

  

猜你喜欢

转载自www.cnblogs.com/jyroy/p/9438622.html
ABS