link:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4027

题意:

  有一个括号序列,每个括号对应一个值,现在可以使得相邻的()进行交换,并得到两个值的乘积,问最后能得到的最大值。

思路:

  从后向前考虑,取后缀最大值。

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define fi first
#define se second typedef long long ll;
typedef pair<int, int> pii; const int inf = 0x3f3f3f3f;
const int maxn = 1e3+; char str[maxn];
ll dp[maxn],mul[maxn],a[maxn];
int main(){
int T; scanf("%d", &T);
while(T--) {
int n; scanf("%d", &n);
scanf("%s", str+);
for(int i=; i<=n; i++) scanf("%lld", &a[i]);
for(int i=; i<=n; i++) dp[i] = ;
ll ans = ;
for(int i=n; i>=; i--) {
if(str[i] == '(') {
ll sum = ;
for(int j=; j<=n; j++) mul[j] = ;
for(int j=i; j<=n; j++) {
if(str[j] == ')')sum += a[i] * a[j];
mul[j] = sum;
}
ll mx = -2e18;
for(int j=n; j>; j--) {
mx = max(mx, dp[j]);
dp[j] = mx + mul[j];
ans = max(ans, dp[j]);
}
}
}
printf("%lld\n", ans);
}
return ;
}

ZOJ4027 Sequence Swapping DP的更多相关文章

  1. 第15届浙江省赛 D Sequence Swapping(dp)

    Sequence Swapping Time Limit: 1 Second      Memory Limit: 65536 KB BaoBao has just found a strange s ...

  2. zoj4027 Sequence Swapping

    首先容易想到二维方程dp(i,j),表示第i个左括号去匹配到第j个右括号时产生的最大值,但如果如此表示的话,首先需要枚举(i,j)以及一个k即dp(i-1,k). 考虑变化dp(i,j)的表示方法,可 ...

  3. ZOJ 4027 Sequence Swapping(DP)题解

    题意:一串括号,每个括号代表一个值,当有相邻括号组成()时,可以交换他们两个并得到他们值的乘积,问你最大能得到多少 思路:DP题,注定想得掉头发. 显然一个左括号( 的最远交换距离由他右边的左括号的最 ...

  4. D:Sequence Swapping

    BaoBao has just found a strange sequence {<, >, <, >, , <, >} of length in his poc ...

  5. 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 ...

  6. Arithmetic Sequence(dp)

    Arithmetic Sequence Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 51  Solved: 19[Submit][Status][We ...

  7. ZOJ1463:Brackets Sequence(间隙DP)

    Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular seque ...

  8. poj 1141 Brackets Sequence 区间dp,分块记录

    Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35049   Accepted: 101 ...

  9. POJ--1699 Best Sequence(DP+dfs)

    Best Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5543 Accepted: 2188 Descrip ...

随机推荐

  1. 最近很火的MySQL:抛开复杂的架构设计,MySQL优化思想基本都在这

    优化一览图 优化 笔者将优化分为了两大类:软优化和硬优化.软优化一般是操作数据库即可:而硬优化则是操作服务器硬件及参数设置. 1.软优化 1)查询语句优化 首先我们可以用EXPLAIN或DESCRIB ...

  2. Nginx配置安装(Mac)

    我用到的安装工具是:homebrew 真的很方便! 步骤1: 打开终端,输入 brew info nginx结果:我们可以看到,nginx在本地还未安装(Not installed),nginx的来源 ...

  3. 【Java例题】3.5 级数之和

    5. 计算级数之和: y=3*1!/1-3^2*2!/2^2+3^3*3!/3^3-...+ (-1)^(n-1)*3^n*n!/n^n. 这里的"^"表示乘方,"!&q ...

  4. asp.net core系列 69 Amazon S3 资源文件上传示例

    一.  上传示例 Install-Package AWSSDK.S3 -Version 3.3.104.10 using Amazon; using Amazon.Runtime; using Ama ...

  5. Hystrix超时测试

    package com.cookie.test; import com.netflix.hystrix.HystrixCommand; import com.netflix.hystrix.Hystr ...

  6. 消息中间件-activemq实战之整合Spring(四)

    前面的理论准备已经很充分,这一节我们来实战:将activemq整合到Spring框架才行中,因为Spring已经集成了JMS,这也为我们配置activermq带来了方便. 1. Spring对jms的 ...

  7. alluxio源码解析-rpc调用概述-client和worker之间的block模块的通讯架构(netty版本)(3)

    (1.8版本)client和worker之间的block模块的通讯架构 block作为alluxio文件读取或者存储的最小基本单位,都是通过BlockOutStream和BlockInputtream ...

  8. ArcGIS数据格式详解

  9. PKI机制总结

    PKI,全称是Public Key Infrastructure,可译为公钥基础设施.它是因特网中节点通信的安全保障机制,HTTPS中的‘S’就来源于PKI. 要去学习一个技术,首先要从它的源头考虑— ...

  10. linux后台运行的几种方式

    1.nohup将程序以忽略挂起信号的方式运行起来 补充说明nohup命令 可以将程序以忽略挂起信号的方式运行起来,被运行的程序的输出信息将不会显示到终端.无论是否将 nohup 命令的输出重定向到终端 ...