zoukankan      html  css  js  c++  java
  • East Central North America 2005-2006——Two Ends

    East Central North America 2005-2006——Two Ends

    题目来源:

    题意:

    有一组数,你和一个人轮流取数(只能从两端取),问你能取到的最大的数和同时对方取到的数的差值。

    题解:

    一道裸的区间DP(顺便学了下区间DP,还是得多刷刷专题才好)

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    #include <cmath>
    #include <set>
    typedef long long ll;
    typedef unsigned long long ull;
    using namespace std;
    const int maxn = 1e6+10;
    int n,dp[1111][1121];
    int arr[1111];
    
    int dfs(int l,int r)
    {
        if(l>r)
            return 0;
        if(l==r)
            return arr[l];
        if(dp[l][r]!=-1)
            return dp[l][r];
        int tl=0,tr=0;
        if(arr[r]>arr[l+1])
            tl=dfs(l+1,r-1)+arr[l];
        else
            tl=dfs(l+2,r)+arr[l];
    
        if(arr[r-1]>arr[l])
            tr=dfs(l,r-2)+arr[r];
        else
            tr=dfs(l+1,r-1)+arr[r];
        return dp[l][r]=(tl>tr)?tl:tr;
    }
    
    int main()
    {
    #ifndef ONLINE_JUDGE
        //freopen("H.in","r",stdin);
        //freopen("H.out","w",stdout);
    #endif
        int cas=0;
        while(~scanf("%d",&n) && n)
        {
            cas++;
            int sum=0;
            memset(arr,0,sizeof(arr));
            memset(dp,-1,sizeof(dp));
            for(int i=0;i<n;i++)
            {
                cin >> arr[i];
                sum+=arr[i];
            }
            int s=dfs(0,n-1);
            int s2=sum-s;
            printf("In game %d, the greedy strategy might lose by as many as %d points.
    ",cas,s-s2);
        }
        return 0;
    }
    
    
  • 相关阅读:
    最小树形图 朱刘算法模板+建边技巧
    模板倍增LCA 求树上两点距离 hdu2586
    【瞎搞题】gym226123 L. For the Honest Election
    【凸包板题】Gym
    集合3
    集合2
    集合1
    常用API&异常
    内部类&API
    多态&接口类&接口
  • 原文地址:https://www.cnblogs.com/Combustible-ice/p/5835943.html
Copyright © 2011-2022 走看看