记忆化搜索超时

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

shs_ryf @ 2018-08-20 18:56:54

为什么记忆化搜索会在第八个点超时

#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<bitset>
typedef long long ll;
using namespace std;
const int INF = 0x3f3f3f3f;

inline int read(){
    char c = getchar();
    int x = 0, f = 1;
    while(c < '0' || c > '9'){
        if(c == '-')
            f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9'){
        x *= 10;
        x += c - '0';
        c = getchar();
    }
    return x * f;
}

int value[1007][1007], f[1007][1007], n;

//time limit exceeded!!
/*
int dfs(int x, int y){
    if(x == n)
        return value[x][y];
    if(f[x][y])
        return f[x][y];
    f[x][y] = max(dfs(x + 1, y), dfs(x + 1, y + 1)) + value[x][y];
    return f[x][y];
}
*/

int main(){
    n = read();
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= i; j++){
            value[i][j] = read();
        }
    }
    for(int j = 1; j <= n; j++)
        f[n][j] = value[n][j];
    for(int i = n - 1; i > 0; i--){
        for(int j = 1; j <= i; j++){
            f[i][j] = max(f[i + 1][j], f[i + 1][j + 1]) + value[i][j];
        }
    }
    printf("%d\n", f[1][1]);
//  printf("%d\n", dfs(1, 1));
    return 0;
}

注释内的是超时的dfs记忆化搜索


by Sudohry @ 2020-10-04 19:25:33

啊这?就一定要字符读入吗,,,直接读数啊


|