• [搜索]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≤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.

    思路:估计只有我用这种蠢办法了把?。。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
    ",dfs(1,z,0));
        return 0;
    }

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

    转载请注明出处:https://www.cnblogs.com/lllxq/
  • 相关阅读:
    微软开发中心的rss历史记录(22)
    asp.net dll 动态生成和调用(转)
    准备写博了
    来博客园报道啦
    web爬行器的准备工作
    跨浏览器设置标签样式
    感谢我身边的朋友们
    难过的一天:(
    12月:期待好运来
    11月 难过一整个世界都寂寞
  • 原文地址:https://www.cnblogs.com/lllxq/p/9439009.html
Copyright © 2020-2023  润新知