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/
  • 相关阅读:
    Bzoj 3173: [Tjoi2013]最长上升子序列 平衡树,Treap,二分,树的序遍历
    Bzoj 1657: [Usaco2006 Mar]Mooo 奶牛的歌声 单调栈
    Bzoj 1391: [Ceoi2008]order 网络流,最大权闭合图
    Bzoj 1674: [Usaco2005]Part Acquisition dijkstra,堆
    Bzoj 3110: [Zjoi2013]K大数查询 树套树
    Cogs 309. [USACO 3.2] 香甜的黄油 dijkstra,堆,最短路,floyd
    面试题24:二叉排序树的后序遍历序列
    面试题23:从上往下打印二叉树
    面试题22:栈的压入、弹出序列
    面试题21:包含min函数的栈
  • 原文地址:https://www.cnblogs.com/lllxq/p/9439009.html
Copyright © 2011-2022 走看看