zoukankan      html  css  js  c++  java
  • A Game

    A Game

    时间限制:10000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    Little Hi and Little Ho are playing a game. There is an integer array in front of them. They take turns (Little Ho goes first) to select a number from either the beginning or the end of the array. The number will be added to the selecter's score and then be removed from the array.

    Given the array what is the maximum score Little Ho can get? Note that Little Hi is smart and he always uses the optimal strategy. 

    输入

    The first line contains an integer N denoting the length of the array. (1 ≤ N ≤ 1000)

    The second line contains N integers A1A2, ... AN, denoting the array. (-1000 ≤ Ai ≤ 1000)

    输出

    Output the maximum score Little Ho can get.

    样例输入
    4
    -1 0 100 2
    样例输出
    99
    分析:枚举区间长度和起点,动态规划
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #include <ext/rope>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define vi vector<int>
    #define pii pair<int,int>
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    const int maxn=1e3+10;
    const int dis[][2]={0,1,-1,0,0,-1,1,0};
    using namespace std;
    using namespace __gnu_cxx;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    int n,m,dp[maxn][maxn],a[maxn],sum[maxn];
    int main()
    {
        int i,j,k,t;
        scanf("%d",&n);
        rep(i,1,n)scanf("%d",&a[i]),dp[i][1]=a[i],sum[i]=sum[i-1]+a[i];
        rep(i,2,n)
        {
            for(j=1;j+i-1<=n;j++)
            {
                dp[j][i]=max(sum[j+i-1]-sum[j-1]-dp[j][i-1],sum[j+i-1]-sum[j-1]-dp[j+1][i-1]);
            }
        }
        printf("%d
    ",dp[1][n]);
        //system("pause");
        return 0;
    }
     
  • 相关阅读:
    linux下好用的软件
    linux下常见目录的介绍
    slam相关链接
    GPS 使用的三种坐标系 ECEF、ENU、LLA坐标系
    平面度计算 C++实现
    QT Cmake添加srv和msg
    pcl点云与ros之间的数据类型转换
    ROS分布式设置
    qt中播放视频和音频
    linux tips
  • 原文地址:https://www.cnblogs.com/dyzll/p/5666104.html
Copyright © 2011-2022 走看看