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】服务无法启动(Mac)

    如图所示: 点击 Start MySQL Server 没反应-- 终端输入 mysql 命令时报错如下: ERROR 2002 (HY000): Can't connect to local MyS ...

  2. Thymeleaf 模板 springboot集成使用

    一.Thymeleaf是什么? 简单说, Thymeleaf 是一款用于渲染XML/XHTML/HTML5内容的模板引擎,类似我之前用过的FreeMarker .由于它支持 html 原型,然后在 h ...

  3. 运行sh文件

    记下在Ubuntu下安装*.sh和*.bin的简单方法. *.sh文件安装方法: 运行终端到文件目录下 1.在终端输入:sudo sh *.sh直接运行 2.在终端输入:sudo chmod +x * ...

  4. 初识Apache NiFi

    一. NiFi介绍 Apache NiFi支持功能强大且可扩展的数据路由,转换和系统中介逻辑的有向图. Apache NiFi的一些高级功能和目标包括: 基于Web的用户界面 设计,控制,反馈和监控之 ...

  5. The introduction of the book American daily English notes (enlarged edition)

    After reading the book of American daily English notes written by Linkun Yang[1], I think I should a ...

  6. java并发编程(八)----(JUC)CountDownLatch

    CountDownLatch 是一个非常实用的多线程控制工具类." Count Down " 在英文中意为倒计数, Latch 为门问的意思.如果翻译成为倒计数门阀, 我想大家都会 ...

  7. 记录一次基于docker搭建jira平台

    背景:项目需要选择不同测试平台,筛选一个最佳平台 现状:一台机器安装多套环境,虚拟机太繁琐 解决方案:通过Docker模拟安装测环境 1.pull docker 镜像: jira:7.11.1(目前的 ...

  8. 解决Springboot整合ActiveMQ发送和接收topic消息的问题

    环境搭建 1.创建maven项目(jar) 2.pom.xml添加依赖 <parent> <groupId>org.springframework.boot</group ...

  9. Redis之对象篇——Redis对象系统简介

    Redis之对象篇--Redis对象系统简介 前言     之前几篇文章,简单介绍 Redis用到的所有主要数据结构,简单动态字符串(SDS).双端链表.字典.压缩列表.整数集合.跳跃表. 图解Red ...

  10. RDIFramework.NET敏捷开发框架通过SignalR技术整合即时通讯(IM)

    1.引言 即时通讯(IM)是RDIFramework.NET敏捷开发框架全新提供的一个基于Web的即时通讯.内部聊天沟通的工具.界面美观大方对于框架内部进行消息的沟通非常方便.基于RDIFramewo ...