zoukankan      html  css  js  c++  java
  • [搜索]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/
  • 相关阅读:
    lambba表达式
    根据某个字段筛选list数据
    git和idea绑定
    gitee创建仓库
    用 Python 3 + PyQt5 擼了一個可以播放“任意”音樂的播放器
    zabbix 共享内存设置
    Zabbix高可用,实现zabbix的无缝切换,无故障时间
    python练习题100
    斐波那契数列python实现
    随机生成指定位数密码包括大小写数字特殊符号
  • 原文地址:https://www.cnblogs.com/lllxq/p/9439009.html
Copyright © 2011-2022 走看看