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建造,则要总 ...
随机推荐
- C++之易混淆知识点三---算法分析
最近复习算法,感到有一丝丝忘记的困惑,赶紧记下来... 一.分治法 分治法的思想就是“分而治之”,很明显就是将规模比较庞大.复杂的问题进行分治,然后得到多个小模块,最好这些小模块之间是独立的,如果这些 ...
- [转]m3u8直播测试地址
http://www.cnblogs.com/yuandaozhe/p/5755453.html 调试m3u8的时候需要测试地址 找了几个,备用一下 安徽卫视 http://stream2.ahtv. ...
- JS 判断中英文字符长度
function strlen(str) { var len = 0; for (var i = 0; i < str.length; i++) { ...
- Dapper基础知识一
在下刚毕业工作,之前实习有用到Dapper?这几天新项目想用上Dapper,在下比较菜鸟,这块只是个人对Dapper的一种总结. 1,什么是Dapper? Dapper,.NET下的一种ORM ...
- 捕捉soap的xml形式
下面是我以前对Php的soap接口进行抓包分析出的结果,这个分析在当服务端或者客户端的Php没有安装soap模块时,可以使用构建xml的方式实现相同的功能 服务端: $data = $HTTP_RAW ...
- 利用MFC创建窗口、消息映射、window中的字节
利用MFC创建窗口: 1.mfc的头文件:afxwin.h 2.自定义类,继承于CWinApp,应用程序类(app应用程序对象,有且仅有一个) 3.程序入口:Initinstance 4.在程序入口中 ...
- HDU-2222 Keywords Search 字符串问题 AC自动机
题目链接:https://cn.vjudge.net/problem/HDU-2222 题意 给一些关键词,和一个待查询的字符串 问这个字符串里包含多少种关键词 思路 AC自动机模版题咯 注意一般情况 ...
- 关于多线程lock-free代码
首先要理解JVM内存模型,可以参考我之前的文章. 然后C++里面其实有一样的指令排序的问题.虽然C++11里面针对happens-before规则做了一些语义上面的支持.但是普通C/C++没有做这些支 ...
- 50个Android开发技巧(09 避免用EditText对日期进行验证)
我们都知道,在表单中对数据进行验证不但无聊并且easy出错. (原文地址:http://blog.csdn.net/vector_yi/article/details/24424713) 想象一下,一 ...
- php冒泡排序函数
$arr=array(23,5,26,4,9,85,10,2,55,44,21,39,11,16,55,88,421,226,588); function maopao($arr,$value){// ...