好坑啊,最后一个点超时,大佬帮忙看一下

P1216 [USACO1.5] [IOI1994]数字三角形 Number Triangles

qi_f @ 2018-02-26 19:00:21

亲测最后测试点答案对上了,时间也没超过1S

import java.util.Scanner;

public class Main {

    public static void main(String args[]){

        Scanner sr = new Scanner(System.in);
        int n=sr.nextInt();
        int a[][]=new int[n+1][n+1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                a[i][j]=sr.nextInt();       
            }
        }
        int max=0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                a[i][j]+=Math.max(a[i-1][j], a[i-1][j-1]);
                max=Math.max(max, a[i][j]);
            }
        }
        System.out.println(max);
    }           
}

by qi_f @ 2018-02-26 19:02:51

还要如何优化啊


by Flaranis @ 2018-02-26 19:18:28

换语言2333 Java速度不敢恭维


by qi_f @ 2018-02-26 19:28:41

@流风之回雪 不知道为什么我都把转换公式那航放在输入的循环里面,时间竟然不会减少``` for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { a[i][j]=sr.nextInt(); a[i][j]+=Math.max(a[i-1][j], a[i-1][j-1]);
} }


by qi_f @ 2018-02-26 19:31:03


import java.util.Scanner;

public class Main {

    public static void main(String args[]){
        Scanner sr = new Scanner(System.in);
        int n=sr.nextInt();
        int a[][]=new int[n+1][n+1];
        int max=0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                a[i][j]=sr.nextInt();
                a[i][j]+=Math.max(a[i-1][j], a[i-1][j-1]);      
            }
        }
        for (int i = 1; i <= n/2+1; i++) {
            int x=Math.max(a[n][i],a[n][n+1-i]);
            if(x>max) max=x;
        }
        System.out.println(max);
    }           
}
```改成这样时间都没减少,有点假

by 404_notfound @ 2018-08-07 09:39:43

Java实在是慢qwq


|