为什么记搜TLE

P3146 [USACO16OPEN] 248 G

@[心余无痕](/user/305002) 把 dp 数组的初值改成 $-1$,因为数组中可能有大量的 $0$,导致重复搜索。
by Alan_Zhao @ 2021-08-24 21:34:15


@[Alan_Zhao](/user/225625) 好像还是T
by StormyEpisode @ 2021-08-24 21:36:50


@[心余无痕](/user/305002) 我改完就过了,你应该改错了
by Alan_Zhao @ 2021-08-24 21:39:08


@[心余无痕](/user/305002) ```cpp #include <bits/stdc++.h> using namespace std; const int N = 300; int n, a[N], dp[N][N]; // 这里考虑只转移能够合并的 int solve(int L, int R) { if(dp[L][R]!=-1) return dp[L][R]; if(L == R) return a[L]; int res = 0; for(int i = L; i < R; i ++ ) { int s1 = solve(L, i), s2 = solve(i + 1, R); if(!s1 || !s2) continue; if(s1 == s2) res = max(res, s1 + 1); } return dp[L][R] = res; } int main() { memset(dp,-1,sizeof dp); cin >> n; for(int i = 1; i <= n; i ++ ) cin >> a[i]; int res = 0; solve(1, n); for(int i = 1; i <= n; i ++ ) for(int j = i; j <= n; j ++ ) { res = max(res, dp[i][j]); #ifdef debug cout << "[" << i << ", " << j << "]: " << solve(i, j) << endl; #endif } cout << res << endl; return 0; } ```
by Alan_Zhao @ 2021-08-24 21:39:51


好吧我太菜了![](//图.tk/9)
by StormyEpisode @ 2021-08-24 21:40:08


我NC写了个`if dp[l, r] > 0 return`
by StormyEpisode @ 2021-08-24 21:43:06


|