思路:

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

代码:

  

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <cctype>
#include <time.h> using namespace std; typedef __int64 ll; const int INF = <<;
const int MAXN = (int) ; inline void nextInt(int &x) {
char c = getchar();
x = ;
while (isdigit(c)) {
x = x* + c-'';
c = getchar();
}
} inline void nextLL(ll &x) {
char c = getchar();
x = ;
while (isdigit(c)) {
x = x* + c-'';
c = getchar();
}
} ll a[MAXN], V[MAXN], prefix[MAXN], suffix[MAXN];
ll dp[MAXN];
int sym[MAXN];
int n; void solve() {
a[] = ;
prefix[] = suffix[n+] = ;
for (int i = ; i <= n; i++) prefix[i] = suffix[i] = V[i];
for (int i = ; i < n; i++) prefix[i+] += prefix[i]; //前缀和
for (int i = n; i > ; i--) suffix[i] += suffix[i+]; //后缀和 for (int i = , j = n; i <= n; i++) { //求对称点
sym[i] = -;
while (j> && prefix[i]>suffix[j]) j--;
if (prefix[i]==suffix[j]) sym[i] = j;
} memset(dp, -, sizeof(dp));
for (int i = ; i <= n; i++) if (sym[i]>) { //这一点有对称点
if (sym[i] <= i) break; //枚举过界
dp[i] = a[i] + a[n-sym[i]+]; //前面是一整段
for (int j = ; j < i; j++) if (sym[j]>) { //从j转移过来
dp[i] = min(dp[i], dp[j]+a[i-j]+a[sym[j]-sym[i]]);
}
} ll ans = a[n];
for (int i = ; i <= n; i++) if (dp[i]>=)
ans = min(ans, dp[i]+a[sym[i]-i-]); //中间合成一段
printf("%I64d\n", ans);
} int main() {
#ifdef Phantom01
freopen("HDU4960.txt", "r", stdin);
#endif //Phantom01 while () {
nextInt(n);
if (n==) break;
for (int i = ; i <= n; i++)
nextLL(V[i]);
for (int i = ; i <= n; i++)
nextLL(a[i]);
solve();
} return ;
}

HDU 4960 Another OCD Patient 简单DP的更多相关文章

  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 (最短路 解法

    http://acm.hdu.edu.cn/showproblem.php?pid=4960 2014 Multi-University Training Contest 9 Another OCD ...

  4. HDU 5375 Gray code (简单dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5375 题面: Gray code Time Limit: 2000/1000 MS (Java/Oth ...

  5. hdu 2084 数塔(简单dp)

    题目 简单dp //简单的dp #include<stdio.h> #include<string.h> #include<algorithm> using nam ...

  6. hdu 2041 超级楼梯(简单dp)

    超级楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. [hdu4960]Another OCD Patient(区间dp)

    题意:给出n个数,把这n个数合成一个对称的集合.每个数只能合并一次. 解题关键:区间dp,dp[l][r]表示l-r区间内满足条件的最大值.vi是大于0的,所以可以直接双指针确定. 转移方程:$dp[ ...

  8. HDU 4939 Stupid Tower Defense 简单DP

    题意: 地图为长为n个单位长度的直线,每通过一个单位长度需要t秒. 有3种塔,红塔可以在当前格子每秒造成x点伤害,绿塔可以在之后格子造成y点伤害,蓝塔可以使通过单位长度的时间增加z秒. 让你安排塔的排 ...

  9. HDU 6024 Building Shops (简单dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6024 题意:有n个room在一条直线上,需要这这些room里面建造商店,如果第i个room建造,则要总 ...

随机推荐

  1. Java事件处理机制1

    实现一个小程序,怎样通过点击不同的按钮,让面板的背景色发生相应的变化,如图: public class Demo2 extends JFrame implements ActionListener{ ...

  2. spline interpolation and draw image by matplotlib

    1 # spline interpolation and draw image by matplotlib from scipy import interpolate import matplotli ...

  3. css3 -阻止元素成为鼠标事件目标 pointer-events

    pointer-events:auto|none 其中pointer-events:none:元素永远不会成为鼠标事件的target. <!DOCTYPE html> <html l ...

  4. 我所认识的EXT2(一)

    前言: 本文是笔者自己在学习文件系统中的一些体会,写出来和大家分享一下.本文首先是介绍了下文件系统的一些理论概念,然后分析了ext2文件系统的原理和部分源码. 文件系统是什么: 人们在认识一件陌生事物 ...

  5. LAMP 建立 Wordpress 站点 Linux Apache MariaDB PHP

    使用LAMP建立Wordpress, 要求如下: 准备工作: VMware 14 CentOS 7.4 最小化 安装镜像 Wordpress 安装包,  下载 预热: 使用VMware新建4台虚拟机, ...

  6. [Debug]SpaceVim中neomake报错 Error while trying to load a compilation database

    回家装上archlinux,突发奇想装个SpaceVim写题 安装配置一路可以说是没有太大问题 最后在写题时出现如下问题 Error while trying to load a compilatio ...

  7. java redistemplate

    //添加一个 key ValueOperations<String, Object> value = redisTemplate.opsForValue(); value.set(&quo ...

  8. C语言声明语句

    设计理念: C语言的一个设计理念就是声明变量和使用变量的形式应该是一致的 优点:声明变量和使用变量时的运算符优先级是相同的 缺点:运算符的优先级是C语言过度解析的部分之一 术语: 变量声明中使用到的符 ...

  9. /etc/rc.d/rc.sysinit

    [root@web02 ~]# ls /etc/rc.d/rc.sysinit /etc/rc.d/rc.sysinit [root@web02 ~]# [root@web02 ~]# ls /etc ...

  10. mycat读写分离+垂直切分+水平切分+er分片+全局表 测试

    原文http://blog.163.com/bigoceanwu@126/blog/static/172718064201683031639683/ 读写分离:利用最基础的mysql主从复制,事务性的 ...