http://acm.hdu.edu.cn/showproblem.php?pid=4960

2014 Multi-University Training Contest 9

Another OCD Patient

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 181    Accepted Submission(s): 58

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 symmetrical! For
example, (10, 20, 20, 10), (4,1,4) and (2) are symmetrical but (3,1,2),
(3, 1, 1) and (1, 2, 1, 2) are not.

However, because Xiaoji's
OCD is more and more serious, 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 would not use it to merge again. Don't ask
why. You should 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
5
6 2 8 7 1
0 5 2 10 20
0
 
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.

 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  4970 4969 4968 4967 4966

题意:给出一个数列,包含n个数字v[i],再给出一个代价表a[j]。可以将v[]中相邻的j个数字合并(相加)为一个数,代价为a[j],每个v[i]最多被合并一次(新生成的数字不能被合并)。求 将v[]合并为一个回文串 所需的最小代价。

题解:

必须要把v[]合成回文串,所以最左边的若干个数合成,要等于最右边的若干个数合成。我们可以设两个下标l和r,表示左右各包括哪里了,若左边的和小于右边的和,l++,否则r--,直到左边的和等于右边的和,这时左右就分出了一组和相同的组合,我们只需要分别记录两边的元素个数。然后对[l+1,r-1]这个中间的区间,继续这个操作。最后中间可能会剩下一些数,将其元素个数记为mid。

然后我们就得到了很多组个数,用这些就可以算出答案。因为可能的合成方案就是我们这些组各合各的,最后就能组成一个回文串;或者其中相邻的若干组合成,因为各组本来就是对称相等的,相邻的组合成也是相等的;还有就是中间的mid和靠近中间的若干组合成。

这个最优结果怎么求呢?我想到了最短路。建个图,把合成操作当做路加入图中,i->j的路表示把第i+1,i+2,...j组合成为一组,路的长度为代价(可由各组的元素个数和求得,个数和可以先预处理,以便快速求得)。i->i+1代表这一组单独用,不合成,路的长度为代价。再弄一点把中间那块mid和其他的合成的路。

然后d[i]为0到i的最短路,也就是处理完第i组的最小消耗,d[处理完中间那块]就是答案。

不过要600ms,看来还是不如标准做法,我怕了,日后再学标准的。

代码:

 //#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usll unsigned ll
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) prllf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout)
#define mp make_pair
#define pb push_back const int maxn=;
const int INF=*(1e9);
ll v[maxn];
int a[maxn];
int n;
//vector<int>SL,SR;
//int mid; const int maxm=maxn*maxn/;
struct edge {
int v,next;
int w;
} e[maxm];
int head[maxn],en; inline void add(const int &x,const int &y,const int &z) {
e[en].w=z;
e[en].v=y;
e[en].next=head[x];
head[x]=en++;
} bool f[maxn];///入队标志
int b[maxn];
int d[maxn];///b为循环队列,d为起点到各点的最短路长度
void spfa(int n) { ///0~n-1 共n个点
int i,k;
int st=, l=, r=; memset(f,,sizeof(f));
memset(b,,sizeof(b));
for(i=; i<n; i++)
d[i]=INF;
b[]=st;
f[st]=;
d[st]=;
while(l!=r) {
k=b[l++];
l%=n;
for(i=head[k]; i!=-; i=e[i].next)
if (d[k]+e[i].w < d[e[i].v]) {
d[e[i].v]=d[k] + e[i].w;
if (!f[e[i].v]) {
if(d[e[i].v]>d[b[l]]) {///SLF优化
b[r++]=e[i].v;
r%=n;
} else {
l--;
if(l==-)l=n-;
b[l]=e[i].v;
}
f[e[i].v]=;
}
}
f[k]=;
}
} void init() {
memset(head,-,sizeof(head));
en=;
} int suml[maxn],sumr[maxn];
int ql[maxn],qr[maxn],qn;
int farm() {
if(n==)return ;
int i,j,k; qn=;
int mid=;
int l=,r=n-;
int L=l,R=r;
ll sl=v[l],sr=v[r];
while(l<r) {
while(sl<sr) l++,sl+=v[l];
while(sl>sr) r--,sr+=v[r];
if(sl==sr && l<r) {
ql[qn]=l-L+;
qr[qn]=R-r+;
qn++;
l++;r--;
L=l;R=r;
sl=v[l];sr=v[r];
}
}
mid=R-L+;
// REP(i,qn)printf("%3d",ql[i]);
// puts("");
// REP(i,qn)printf("%3d",qr[i]);
// printf(" mid=%d\n",mid);
mz(suml);
mz(sumr);
for(i=; i<=qn; i++) {
suml[i]=suml[i-]+ql[i-];
sumr[i]=sumr[i-]+qr[i-];
}
init();
for(i=; i<qn; i++)
for(j=i+; j<=qn; j++) {
add(i,j, a[ suml[j] - suml[i] ] + a[ sumr[j]-sumr[i] ] );
}
for(i=; i<=qn; i++) {
add(i, qn+, a[ suml[qn] - suml[i] + sumr[qn] - sumr[i] + mid ]);
}
spfa(qn+);///0为起点,d[i]为搞完第i块的最少消耗
return d[qn+];
} int main() {
int i;
while(scanf("%d",&n)!=EOF) {
if(n==)break;
REP(i,n)scanf("%I64d",&v[i]);
FOR(i,,n)scanf("%d",&a[i]);
printf("%d\n",farm());
}
return ;
}

hdu 4960 Another OCD Patient (最短路 解法的更多相关文章

  1. HDU 4960 Another OCD Patient(记忆化搜索)

    HDU 4960 Another OCD Patient pid=4960" target="_blank" style="">题目链接 记忆化 ...

  2. hdu 4960 Another OCD Patient(dp)

    Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Ot ...

  3. HDU 4960 Another OCD Patient 简单DP

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

  4. HDU4960Another OCD Patient(间隙dp,后座DP)

    Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Ot ...

  5. hdu 4960 记忆化搜索 DP

    Another OCD Patient Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Ot ...

  6. HDU 4960 (水dp)

    Another OCD Patient Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. Th ...

  7. HDU 4063 Aircraft --几何,最短路

    题意: 给一些圆,要求从第一个圆的圆心走到最后一个圆的圆心,中间路径必须在某个圆内,求最短路径的长度. 解法: 易知要保持在圆内且路径最短,走两圆相交的点能使路径尽量短,所以我们找出所有的两圆相交的点 ...

  8. HDU 5637 Transform 单源最短路

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5637 题意: http://bestcoder.hdu.edu.cn/contests/contes ...

  9. HDU 4606 Occupy Cities (计算几何+最短路+最小路径覆盖)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题目:给出n个城市需要去占领,有m条线段是障碍物, ...

随机推荐

  1. BZOJ1070 [SCOI2007]修车

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000作者博客:http://www.cnblogs.com/ljh2000-jump/转 ...

  2. C++11之thread线程

    今天由于项目需求(其实是某门课的一个大作业,不好意思说出口啊...),想要使用多线程.相信大家一般用的是linux上的POSIX C或windows上的线程库,然而这些线程库以来于特定系统,并不“标准 ...

  3. lua的corroutine学习

    lua的corroutine学习 function receive (prod) local status, value = coroutine.resume(prod) return value e ...

  4. Zabbix网络自动发现规则和自动添加hosts及link模板

    Version: zabbix 3.0 一.配置网络发现规则 Device uniqueness criteria:选择主机名作为唯一标识(Configuation Hosts中显示的NAME) 二. ...

  5. iOS Keychain钥匙串,应用间数据共享打造iOS上的全家桶

    Demo先行:https://github.com/rayshen/GIKeychainGroupDemo 该demo里有2个工程,你先运行任何一个会存储一个值,再运行另一个会访问之前的app存储的值 ...

  6. android 事件传递机制 心得

    看了网上很多资料. 最后我发现可以用很简单的几句话就能把它说清楚 1 每个 viewgroup 内都有 三个方法 a dispatchTouchEvent 是自己决定要不要(管他爹)要这个苹果的 一般 ...

  7. HDU 5726 GCD

    传送门 GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem ...

  8. Java 代码性能优化总结

    前言 代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用, ...

  9. Apache Tomcat目录下各个文件夹的作用

    1.bin:存放各种不同平台开启与关闭Tomcat的脚本文件. 2.lib:存tomcat与web应用的Jar包. 3.conf:存放tomcat的配置文件. 4.webapps:web应用的发布目录 ...

  10. iOS开发-二维码

    二维码 从ios7开始集成了二维码的生成和读取功能 此前被广泛使用的zbarsdk目前不支持64位处理器 生成二维码的步骤: 倒入CoreImage框架 通过滤镜CIFilter生成二维码 二维码的内 ...