ZOJ 4027 Sequence Swapping(DP)题解
题意:一串括号,每个括号代表一个值,当有相邻括号组成()时,可以交换他们两个并得到他们值的乘积,问你最大能得到多少
思路:DP题,注定想得掉头发。
显然一个左括号( 的最远交换距离由他右边的左括号的最终位置决定,那么我们可以从右边开始做。我们用dp[i][j]表示第i个左括号交换到第j个位置后,他和他后面左括号所能得到的最大值。显然,dp[i][j] = i交换得到的值 + 后面左括号产生的最大值。而后面左括号能产生的最大值显然就是max(dp[i+1][k])其中j <= k <= n。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e3 + ;
const int INF = 0x3f3f3f3f;
char s[maxn];
ll v[maxn], n, Max, pos[maxn], sum[maxn]; ///Max[i]定义为后面的交换到i的最大值
ll dp[maxn][maxn]; ///第i个放在j位置得到的最大值
int main(){
int t, cnt;
scanf("%d", &t);
while(t--){
cnt = ;
scanf("%lld", &n);
scanf("%s", s + );
sum[] = ;
for(int i = ; i <= n; i++){
scanf("%lld", &v[i]);
if(s[i] == ')') sum[i] = sum[i - ] + v[i];
else pos[++cnt] = i, sum[i] = sum[i - ];
}
memset(dp, , sizeof(dp));
for(int i = cnt; i >= ; i--){
int u = pos[i];
Max = -INF;
for(int j = n; j >= u; j--){
Max = max(Max, dp[i + ][j]);
dp[i][j] = (sum[j] - sum[u - ]) * v[u] + Max;
}
}
ll ans = -INF;
for(int i = ; i <= n; i++)
ans = max(ans, dp[][i]);
printf("%lld\n", ans);
}
return ;
}
ZOJ 4027 Sequence Swapping(DP)题解的更多相关文章
- 第15届浙江省赛 D Sequence Swapping(dp)
Sequence Swapping Time Limit: 1 Second Memory Limit: 65536 KB BaoBao has just found a strange s ...
- ZOJ4027 Sequence Swapping DP
link:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4027 题意: 有一个括号序列,每个括号对应一个值,现在可以使得相 ...
- [JSOI2008]Blue Mary的战役地图——全网唯一一篇dp题解
全网唯一一篇dp题解 网上貌似全部都是哈希+二分(反正我是大概baidu了翻了翻)(还有人暴力AC了的..) 哈希还是相对于dp还是比较麻烦的. 而且正确性还有可能被卡(当然这个题不会) 而且还容易写 ...
- ZOJ Problem Set - 3822Domination(DP)
ZOJ Problem Set - 3822Domination(DP) problemCode=3822">题目链接 题目大意: 给你一个n * m的棋盘,每天都在棋盘上面放一颗棋子 ...
- Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp
C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...
- Arithmetic Sequence(dp)
Arithmetic Sequence Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 51 Solved: 19[Submit][Status][We ...
- D:Sequence Swapping
BaoBao has just found a strange sequence {<, >, <, >, , <, >} of length in his poc ...
- poj 1141 Brackets Sequence (区间dp)
题目链接:http://poj.org/problem?id=1141 题解:求已知子串最短的括号完备的全序列 代码: #include<iostream> #include<cst ...
- [原]POJ1141 Brackets Sequence (dp动态规划,递归)
本文出自:http://blog.csdn.net/svitter 原题:http://poj.org/problem?id=1141 题意:输出添加括号最少,并且使其匹配的串. 题解: dp [ i ...
随机推荐
- 54. Spiral Matrix(剑指offer 19)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- form的action属性值对应servlet的web.xml的url-pattern
<form action="abc">在web.xml里面<servlet><servlet-name>123</servlet-name ...
- jQuery筛选--hasClass(class)和eq(index|-index)
hasClass(class) 概述 检查当前的元素是否含有某个特定的类,如果有,则返回true 参数 class 用于匹配的类名 <!DOCTYPE html> <html> ...
- Shell Necklace (dp递推改cdq分治 + fft)
首先读出题意,然后发现这是一道DP,我们可以获得递推式为 然后就知道,不行啊,时间复杂度为O(n2),然后又可以根据递推式看出这里面可以拆解成多项式乘法,但是即使用了fft,我们还需要做n次多项式乘法 ...
- 什么是Unicode
写这篇博客的原因, 从做软件开始,什么ASCII码, Unicode,UTF-8,UTF-16,UTF-32......这些鬼东西总是经常碰到,只知道这些鬼是编码格式,其他的就啥都不清楚了,既然总是遇 ...
- Struts2输入校验(XML方式)
本章主要介绍struts2的XML配置方式输入校验.以下将结合一个实例程序进行说明. 代码结构: 关键代码: RegistAction.javapackage com.alfred.regist.ac ...
- MyEclipse如何配置Struts2源码的框架压缩包
1.MyEclipse如何配置Struts2源码的框架压缩包 如本机的Struts2框架压缩包路径为:D:\MyEclipseUserLibraries\struts\struts-2.3.15.3- ...
- OLED屏幕那些次像素有趣的排列方式
http://www.dzsc.com/data/2016-6-2/109856.html 我们今天的重点内容为倒数第二列内容的上半部分,也就是RGB排列和Pentile排列.在介绍OLED屏幕时候我 ...
- Eloquent JavaScript #06# class
索引 Notes this Prototype 类 class符号 覆盖派生属性 Maps Symbols iterator接口 Getters, setters, and statics 继承 in ...
- H5+JS生成验证码
效果图如下: <canvas id="canvas1" style="margin-left: 200px;"></canvas>< ...