HDU 4960 Another OCD patient
Question Link
Memory-based search, because each shard value is a positive number, so each prefix and suffix are incremented, you can use twopointer to find each equal position, and then the next interval is equivalent to a subproblem, it can be searched with memory, and the complexity is close to O (N ^ 2)
Code:
# include
# include
# include
using namespace STD; const int INF = 0x3f3f3f3f; const int n= 5005; typedef long ll; int n, a [n], DP [N] [N]; ll V [N], pre [N]; void Init () {for (INT I = 1; I <= N; I ++) {scanf ("% i64d", & V [I]); pre [I] = pre [I-1] + V [I];} For (INT I = 1; I <= N; I ++) scanf ("% d", & A [I]); memset (DP,-1, sizeof (DP);} int solve (INT L, int R) {If (DP [l] [r]! =-1) return DP [l] [r]; DP [l] [r] = A [R-l + 1]; If (L> = r) return DP [l] [r] = 0; int now = L; for (INT I = r; I> = L; I --) {While (pre [now]-pre [L-1]