[搜索]ABS

题目描述

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≤10 9

输入

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.

思路:估计只有我用这种蠢办法了把?。。dfs+记忆化;
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#define lowbit(x) x&(-x)
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;

ll n,z,w,a[2010];
ll f[2010][2];//f[i][0]表示X在面临“i状态”(当前牌堆顶是第i张牌)时的最优答案,f[i][1]表示Y在面临“i状态”时的最优答案

ll dfs(ll layer,ll last,bool player){//last表示另一个人的手牌
  if(f[layer][player]) return f[layer][player];
  if(layer==n) {
    if(player==0) return f[n][0]=abs(a[n]-w);
    else return f[n][1]=abs(last-a[n]);
  }
  if(player==0) return f[layer][player]=max(dfs(layer+1,a[layer],!player),dfs(layer+1,last,player));
  else return f[layer][player]=min(dfs(layer+1,a[layer],!player),dfs(layer+1,last,player));
}

int main()
{
    scanf("%lld%lld%lld",&n,&z,&w);
    for(ll i=1;i<=n;i++) scanf("%lld",&a[i]);
    printf("%lld\n",dfs(1,z,0));
    return 0;
}

我写了个什么怪物啊?。。怕是写数位dp写疯了/

猜你喜欢

转载自www.cnblogs.com/lllxq/p/9439009.html
ABS