Another OCD patient
Time Limit: 2000/1000 MS (Java/others) memory limit: 131072/131072 K (Java/Others)
Total submission (s): 645 accepted submission (s): 238
Problem description
Xiaoji is an OCD (obsessive-compulsive disorder) patient. this morning, his children played with plasticene. they broke the plasticene into N pieces, and put them in a line. each piece has a volume VI. since Xiaoji is an OCD patient, he can't stand with the disorder of the volume of the N pieces of plasticene. now he wants to merge some successive pieces so that the volume in line is already rical! For example, (10, 20, 20, 10), (, 4) and (2) are using rical but (, 2), (3, 1, 1) and (1, 2, 1, 2) are not.
However, because Xiaoji's OCD is more and more serous, now he has a strange opinion that merging I successive pieces into one will cost AI. and he wants to achieve his goal with minimum cost. can you help him?
By the way, if one piece is merged by Xiaoji, He wocould not use it to merge again. Don't ask why. You shoshould know Xiaoji has an OCD.
Input
The input contains multiple test cases.
The first line of each case is an integer N (0 <n <= 5000), indicating the number of pieces in a line. the second line contains N integers VI, volume of each piece (0 <VI <= 10 ^ 9 ). the third line contains N integers AI(0 <AI <= 10000), And A1 is always 0.
The input is terminated by n = 0.
Output
Output one line containing the minimum cost of all operations Xiaoji needs.
Sample Input
56 2 8 7 10 5 2 10 200
Sample output
10
Hint
In the sample, there is two ways to achieve Xiaoji‘s goal.[6 2 8 7 1] -> [8 8 7 1] -> [8 8 8] will cost 5 + 5 = 10.[6 2 8 7 1] -> [24] will cost 20.
Author
Sysu
Source
2014 multi-university training contest 9
During the competition today, I thought of a memory-based search. The time complexity is n ^ 2 logn. But when I think about the reality, it is also the complexity of N ^ 2. Because of this complexity, I am afraid of getting stuck in time, so
I have no confidence in the problem. I found that my train of thought was wrong only after I got my teammate's question.
View code // It is a pity that the competition is based on the idea, 800 ms # include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <queue> using namespace STD; typedef long ll; const int INF = 1 <30; const int n = 5005; int N, DP [N] [N]; ll sum [N], V [N], A [n]; bool vis [N] [N]; int find (int l, int R, ll X) {int n = r; while (L <= r) {int M = (L + r)> 1; if (sum [N]-sum M-1] = x) return m; if (sum [N]-sum M-1]> X) L = m + 1; else r = M-1;} return 0;} int DFS (int l, int R) {If (L> = r) return 0; If (vis [l] [r]) return DP [l] [r]; vis [l] [r] = 1; Int & Ans = DP [l] [r]; ans = V [R-l + 1]; for (INT I = L; I <r; I ++) {int r = find (I + 1, R, sum [I]-sum L-1]); If (! R) continue; ans = min (LL) ans, (LL) (DFS (I + 1, R-1) + V [I-l + 1] + V [R-R + 1]);} return ans;} void solve () {for (INT I = 1; I <= N; I ++) {scanf ("% i64d", A + I); sum [I] = sum [I-1] + A [I];} for (INT I = 1; I <= N; I ++) scanf ("% i64d", V + I); memset (VIS, 0, sizeof (VIS )); printf ("% d \ n", DFS (1, N);} int main () {// freopen ("in.txt", "r", stdin ); while (scanf ("% d", & N)> 0 & N) solve (); Return 0 ;}
According to the competition, the idea is optimized. The idea is similar, but the time space is optimized.
view code#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;const int N = 5010;int L[N], R[N], cnt;ll dp[N], sum[N], a[N], v[N], n;bool vis[N];int find(int l, int r, ll x){ int n = r; while(l<=r) { int m = (l+r)>>1; if(sum[n]-sum[m-1]==x) return m; if(sum[n]-sum[m-1]>x) l=m+1; else r = m-1; } return 0;}void init(){ cnt = 0; for(int l=1; l<=n; l++) { int r = find(l+1, n, sum[l]); if(!r) continue; L[cnt] = l; R[cnt++] = r; }}ll dfs(int pos, int l, int r){ if(l>=r) return 0; if(dp[l]!=-1) return dp[l]; ll& ans=dp[l]; ans = v[r-l+1]; for(int i=pos; i<cnt; i++) { ans = min(dfs(i+1, L[i]+1,R[i]-1)+v[L[i]-l+1]+v[r-R[i]+1], ans); } return ans;}void solve(){ for(int i=1; i<=n; i++){ scanf("%I64d", &a[i]); sum[i] = sum[i-1]+a[i]; } for(int i=1; i<=n; i++) scanf("%I64d", v+i); init(); memset(dp, -1, sizeof(dp)); printf("%I64d\n",dfs(0, 1, n));}int main(){// freopen("in.txt", "r", stdin); while(scanf("%I64d", &n)>0 && n) solve(); return 0;}