0
点赞
收藏
分享

微信扫一扫

HDU 4960(Another OCD Patient-区间dp)

Mezereon 2022-10-25 阅读 82


Another OCD Patient
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1320 Accepted Submission(s): 459

Problem Description
已知一个数列,现在把它分成k段,要求每段的和组成的新数列回文,且合并代价最小。
合并的代价为∑ki=1a[len](len为该段的长度)

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
5
6 2 8 7 1
0 5 2 10 20
0

Sample Output
10

HintIn 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

Recommend
We have carefully selected several similar problems for you: 5431 5430 5429 5428 5427

#include<bits/stdc++.h>
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
typedef __int64 ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+llabs(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int n,a[MAXN],v[MAXN];
ll s[MAXN],s2[MAXN];
int l[MAXN],r[MAXN],tot=0;
ll cost[MAXN];
int main()
{
// freopen("A.in","r",stdin);
// freopen(".out","w",stdout);

while(scanf("%d",&n)&&n) {
For(i,n) scanf("%d",&v[i]);
For(i,n) scanf("%d",&a[i]); a[0]=0;
s[0]=s2[n+1]=0;
For(i,n) s[i]=s[i-1]+(ll)v[i];
ForD(i,n) s2[i]=s2[i+1]+(ll)v[i];

tot=0;
int fro=1,tail=n;
while(fro<tail) {
if (s[fro]==s2[tail]) {
l[++tot]=fro;
r[tot]=tail;
fro++,tail--;
} else if (s[fro]<s2[tail]) ++fro;
else --tail;
}
MEMI(cost)
l[0]=0,r[0]=n+1; cost[0]=0;
ll ans=a[n];
For(i,tot) {
Rep(j,i) {
cost[i]=min(cost[i],cost[j]+a[l[i]-l[j]]+a[r[j]-r[i]]);
}
ans=min(ans,cost[i]+a[r[i]-1-l[i]]) ;
}


cout<<ans<<endl;
}


return 0;
}


举报

相关推荐

0 条评论