HDU 4960(Another OCD Patient-区间dp)

news/2024/10/31 1:33:44/

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 For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])  
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define pb push_back
#define mp make_pair 
#define MAXN (5000+10)
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;
}

http://www.ppmy.cn/news/460671.html

相关文章

HDU 4960 (区间DP)

题目链接&#xff1a;点击这里 题意&#xff1a;把一个数列合并成回文序列&#xff0c;每一个数字只能和并一次&#xff0c;只能连续的子串进行合并&#xff0c;合并后的数字是他们的和&#xff0c;花费是他们中数字个数的函数。给出原始串和花费函数求最小的花费。 O(n) 扫一…

git版本管理入门(本地/远程仓库,常用命令)

目录 git简介 安装git 配置SSH key Linux环境下需要命令生成ssh key 本地git管理 多人协作流程 追加 重新提交 git命令 git commit本地和git push远程 git stash和git stash pop暂存 git status查看修改哪些了文件​ git diff 查看修改前后的差异 git log查看提交…

HDU 4960 Another OCD Patient

解题报告&#xff1a; 维护前缀和以及后缀和&#xff0c;相等则记录为断点 dp[ i ]表示断点 i 中间全部合并&#xff0c;两侧最优合并的最小代价 #include <algorithm> #include <iostream> #include <iomanip> #include <cstring> #include <cli…

【DP】 HDOJ 4960 Another OCD Patient

从两端往中间合并。。。合并过程中的数必然向数小的一方先合并。。。然后我们就可以这样先预处理出关节点。。。然后再关节点上一维DP即可。。。。 #include <iostream> #include <queue> #include <stack> #include <map> #include <set&g…

hdu 4960 Another OCD Patient(动态规划)

题目&#xff1a; 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 Xiaoji is an OCD (obsessive-compulsive disorder) patie…

[dp]HDOJ4960 Another OCD Patient

题意: 给一个n, 第二行给n堆的价值v[i], 第三行给a[i]. a[i]表示把i堆合在一起需要的花费. 求把n堆变成类似回文的 需要的最小花费. 思路: ①记忆化搜索 比较好理解... dp[l][r] 记录l到r的最小花费 枚举对称轴 维护每次l到r之间对称 dp[l][r]min(dp[l][r], a[cur-l]a[r-i]df…

HDU 4960 Another OCD Patient 简单DP

思路&#xff1a; 因为是对称的&#xff0c;所以如果两段是对称的&#xff0c;那么一段的前缀和一定等于另一段的后缀和。根据这个性质&#xff0c;我们可以预处理出这个数列的对称点对。然后最后一个对称段是从哪里开始的&#xff0c;做n^2的DP就可以了。 代码&#xff1a; 1 …

Sicily 4960 Identity Checker

直接用栈模拟&#xff0c;“-”顺序别弄错&#xff0c;不然会WA... #include<iostream> #include<string> #include<cmath> #include<stack> using namespace std; int main() {int n;while (cin >> n && n) {stack<double> stk;s…