zoukankan      html  css  js  c++  java
  • hiho一下 第173周

    题目1 : 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

    AC代码:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int a[1005], dp[1005][1005], s[1005];
    int main() {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    #endif
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
        }
        memset(dp, 0, sizeof(dp));
        s[0] = 0;
        s[1] = a[1];
        for (int i = 2; i <= n; i++) {
            s[i] = s[i - 1] + a[i];
        }
        for (int i = 1; i <= n; i++) {
            dp[i][i] = a[i];
            dp[i][i + 1] = a[i] > a[i + 1] ? a[i] : a[i + 1];
        }
        for (int i = n; i >= 1; i--) {
            for (int j = i + 2; j <= n; j++) {
                dp[i][j] = dp[i + 1][j] > dp[i][j - 1] ? s[j] - s[i - 1] - dp[i][j - 1] : s[j] - s[i - 1] - dp[i + 1][j];
            }
        }
        printf("%d
    ", dp[1][n]);
        return 0;
    }

    WA代码:

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    int a[1005], dp[1005][1005], s[1005];
    int main() {
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
    #endif
        int n;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
        }
        memset(dp, 0, sizeof(dp));
        s[0] = 0;
        s[1] = a[1];
        for (int i = 2; i <= n; i++) {
            s[i] = s[i - 1] + a[i];
        }
        for (int i = 1; i <= n; i++) {
            dp[i][i] = a[i];
            dp[i][i + 1] = a[i] > a[i + 1] ? a[i] : a[i + 1];
        }
        for (int i = 3; i <= n; i++) {
            for (int j = 1; j + i - 1 <= n; j++) {
                int tmp = -10000000;
                //j+1->j+i-1
                if (a[j] + (s[j + i - 1] - s[j]) - dp[j + 1][j + i - 1] > tmp) {
                    tmp = a[j] + (s[j + i - 1] - s[j]) - dp[j + 1][j + i - 1];
                }
                //j->j+i-2
                if (a[j + i - 1] + (s[j + i - 2] - s[j - 1]) - dp[j][j + i - 2] > tmp) {
                    tmp = a[j + i - 1] + (s[j + i - 2] - s[j - 1]) - dp[j][j + i - 2];
                }
                dp[j][j + i - 1] = tmp;
            }
        }
        printf("%d
    ", dp[1][n]);
        return 0;
    }

    思路一样,就是不知道为什么不对。

    几个小时之后,两种都不对了。那你特么倒是解释解释这是怎么回事啊?

    真操蛋,这种问题遇到无数次了。

    OK了,辣鸡OJ评测机有问题。

  • 相关阅读:
    JDK8中的 Lambda 表达式
    IDEA导入新项目jar包以及项目依赖tomcat设置
    idea导入项目,类为灰色,左下角有个红圈
    mysql服务忽然挂了,出现错误信息: Can’t connect to MySQL server on ‘localhost’ (10061)
    mysql、oracle、sql server连接信息
    mybatis中select * 中有字段,自己在起一个别名,然后实体类会使用哪个?
    pymongo的操作
    mongodb备份恢复
    mongodb建立索引
    mongodb聚合命令
  • 原文地址:https://www.cnblogs.com/dramstadt/p/7722826.html
Copyright © 2011-2022 走看看