思路:

  因为是对称的,所以如果两段是对称的,那么一段的前缀和一定等于另一段的后缀和。根据这个性质,我们可以预处理出这个数列的对称点对。然后最后一个对称段是从哪里开始的,做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. ASP.NET MVC 使用FluentScheduler做定时任务

    源代码地址: https://github.com/fluentscheduler/FluentScheduler 使用NuGet安装FluentScheduler 这是我实际项目中用到的代码,也可看 ...

  2. 在sql server数据库的一个表中如何查询共有多少字段

    select a.* from sys.columns a,sys.tables bwhere a.object_id = b.object_id and b.name = '要查的表名'

  3. div内快元素[div,p。。。]居中办法

    方法1: .parent { width:800px; height:500px; border:2px solid #000; position:relative; } .child { width ...

  4. Activiti BPMN 2.0 designer eclipse插件安装

    官方网是这样说的: https://www.activiti.org/userguide/index.html#springSpringBoot The following installation ...

  5. http扩展请求头中的x-Forwarded-For

    X-Forwarded-For格式: X-Forwarded-For: client-ip, proxy1-ip, proxy2-ip 客户端请求服务器的过程中没经过一个代理层(代理中使用了xff)那 ...

  6. SpringCloud学习笔记(10)----Spring Cloud Netflix之声明式 REST客户端 -Feign的高级特性

    1. Feign的默认配置 Feign 的默认配置 Spring Cloud Netflix 提供的默认实现类:FeignClientsConfiguration 解码器:Decoder feignD ...

  7. LeetCode Golang实现 1. 两数之和

    1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这 ...

  8. Fiddler常用配置

    过滤目标地址: 抓取https的设置:

  9. ES 新增字符串方法

    话不多说,直接开鲁 1. startsWith() 作用: 检测字符串以什么开头 实例: let str = "www.qjzzj.top"; console.log(str.st ...

  10. POJ-2253 Frogger dijsktra查找间隔最小的路径

    题目链接:https://cn.vjudge.net/problem/POJ-2253 题意 一只Forg需要从节点1走到节点n 现要找一条各个间隔最小的路径 问间隔最小是多少 思路 用dijsktr ...