Another OCD patient
Time Limit: 2000/1000 MS (Java/others) memory limit: 131072/131072 K (Java/Others)
Total submission (s): 87 accepted submission (s): 24
Problem descriptionxiaoji 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 Lin E is invalid 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.
Inputthe 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.
Outputoutput one line containing the minimum cost of all operations Xiaoji needs.
Sample Input
56 2 8 7 10 5 2 10 200
Sample output
10HintIn 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.
Source2014 multi-university training contest 9
Question and code:
Official question:
The train of thought is the same as that of the official team, but it was not written during the competition. After the competition, I had to repair, modify, and modify it. I am so worried .....
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <map>using namespace std;typedef __int64 ll;ll org[5010],l[5010],r[5010];ll val[5010],d[2510];struct node{ int l,r;} pos[5010];map<ll,int>m;int main(){ int n; while(scanf("%d",&n)&&n) { m.clear(); for(int i=1; i<=n; i++) { scanf("%d",&org[i]); if(i==1) l[i]=org[i]; else l[i]=l[i-1]+org[i]; m[l[i]]=i; } int k=1,flag=1; pos[0].l=pos[0].r=0; for(int i=n; i>=1; i--) { if(i==n) r[i]=org[i]; else r[i]=r[i+1]+org[i]; if(flag&&m.find(r[i])!=m.end()) { pos[k].l=m[r[i]]; pos[k].r=n-i+1; if(pos[k].l+pos[k].r>=n) { flag=0; } k++; } } k--; /*for(int i=0;i<=k;i++) { printf("l:%d r:%d\n",pos[i].l,pos[i].r); } */ int mid; if(pos[k].l+pos[k].r==n) { mid=0; } else { k--; mid=n-pos[k].r-pos[k].l; } //printf("k:%d mid:%d\n",k,mid); val[0]=0; for(int i=1; i<=n; i++) { scanf("%I64d",&val[i]); } ll ans=-1; d[0]=0; for(int i=1; i<=k; i++) { d[i]=val[pos[i].l]+val[pos[i].r]; for(int j=1; j<i; j++) { d[i]=min(d[j]+val[pos[i].l-pos[j].l]+val[pos[i].r-pos[j].r],d[i]); } } for(int i=0; i<=k; i++) { if(ans==-1||ans>d[i]+val[mid+pos[k].l-pos[i].l+pos[k].r-pos[i].r]) ans=d[i]+val[mid+pos[k].l-pos[i].l+pos[k].r-pos[i].r]; } printf("%I64d\n",ans); } return 0;}