HDU 4960 Another OCD Patient 简单DP
思路:
因为是对称的,所以如果两段是对称的,那么一段的前缀和一定等于另一段的后缀和。根据这个性质,我们可以预处理出这个数列的对称点对。然后最后一个对称段是从哪里开始的,做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的更多相关文章
- HDU 4960 Another OCD Patient(记忆化搜索)
HDU 4960 Another OCD Patient pid=4960" target="_blank" style="">题目链接 记忆化 ...
- hdu 4960 Another OCD Patient(dp)
Another OCD Patient Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Ot ...
- hdu 4960 Another OCD Patient (最短路 解法
http://acm.hdu.edu.cn/showproblem.php?pid=4960 2014 Multi-University Training Contest 9 Another OCD ...
- HDU 5375 Gray code (简单dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5375 题面: Gray code Time Limit: 2000/1000 MS (Java/Oth ...
- hdu 2084 数塔(简单dp)
题目 简单dp //简单的dp #include<stdio.h> #include<string.h> #include<algorithm> using nam ...
- hdu 2041 超级楼梯(简单dp)
超级楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- [hdu4960]Another OCD Patient(区间dp)
题意:给出n个数,把这n个数合成一个对称的集合.每个数只能合并一次. 解题关键:区间dp,dp[l][r]表示l-r区间内满足条件的最大值.vi是大于0的,所以可以直接双指针确定. 转移方程:$dp[ ...
- HDU 4939 Stupid Tower Defense 简单DP
题意: 地图为长为n个单位长度的直线,每通过一个单位长度需要t秒. 有3种塔,红塔可以在当前格子每秒造成x点伤害,绿塔可以在之后格子造成y点伤害,蓝塔可以使通过单位长度的时间增加z秒. 让你安排塔的排 ...
- HDU 6024 Building Shops (简单dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6024 题意:有n个room在一条直线上,需要这这些room里面建造商店,如果第i个room建造,则要总 ...
随机推荐
- Sed Awk 日常使用总结
Sed命令语法sed [option] {sed-commands}{input-file}sed首先从input-file中读取第一行,然后执行所有的sed-commands:再读取第二行,执行所有 ...
- swift语言点评五-Function
一.函数类型 Every function in Swift has a type, consisting of the function’s parameter types and return t ...
- Spring Framework 开发参考手册中文(在线HTML)
https://blog.csdn.net/zfrong/article/details/3971722
- [USACO16DEC]Cities and States省市
题目:洛谷P3405. 题目大意:给你一些省市的名称(大写)和所在省的名称(两个大写字母),求有多少对城市满足:A城市的名字的前两个字母等于B城市所在省的名称,且A所在省的名称等于B城市的名字的前两个 ...
- shell 文件中添加内容
下文所有 1111 , 2222 均为字符串 sed -i '/1111/i\2222' a.txt 在a.txt中找到所有符合1111得 前面加上2222 sed -i '/1111/a\2 ...
- You Probably Don’t Need a Message Queue
原文地址 I’m a minimalist, and I don’t like to complicate software too early and unnecessarily. And addi ...
- 2015 Multi-University Training Contest 2 hdu 5306 Gorgeous Sequence
Gorgeous Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- 在Windows上面安装多个Memcached
在Windows上面安装多个Memcached sc create "memcached Server3" start= auto binPath= "D:\memcac ...
- [PHP]怎样在SAE的CodeIgniter项目中隐藏掉index.php
第一步:改动项目根文件夹的config.yaml文件.加入例如以下内容: handle: - rewrite: if(!is_dir() && !is_file() && ...
- hdoj--1237--简单计算器(栈模拟)
简单计算器 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...