D:Sequence Swapping
BaoBao has just found a strange sequence {<, >, <, >, , <, >} of length in his pocket. As you can see, each element <, > in the sequence is an ordered pair, where the first element in the pair is the left parenthesis '(' or the right parenthesis ')', and the second element in the pair is an integer.
As BaoBao is bored, he decides to play with the sequence. At the beginning, BaoBao's score is set to 0. Each time BaoBao can select an integer , swap the -th element and the -th element in the sequence, and increase his score by , if and only if , '(' and ')'.
BaoBao is allowed to perform the swapping any number of times (including zero times). What's the maximum possible score BaoBao can get?
Input
There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:
The first line contains an integer (), indicating the length of the sequence.
The second line contains a string () consisting of '(' and ')'. The -th character in the string indicates , of which the meaning is described above.
The third line contains integers (). Their meanings are described above.
It's guaranteed that the sum of of all test cases will not exceed .
Output
For each test case output one line containing one integer, indicating the maximum possible score BaoBao can get.
Sample Input
4
6
)())()
1 3 5 -1 3 2
6
)())()
1 3 5 -100 3 2
3
())
1 -1 -1
3
())
-1 -1 -1
Sample Output
24
21
0
2
Hint
For the first sample test case, the optimal strategy is to select in order.
For the second sample test case, the optimal strategy is to select in order.
就是一个dp,dp[i][j]表示第i个左括号扩到第j个时候的最大值。
然后更新即可。
但是他没到的不能和他已经到的同等对待。
玛德智障,
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e3+;
char s[N];
int n,p[N],nxt[N];
long long sum[N],v[N];
long long mx[][N];
int main(){
int T;
while(scanf("%d",&T)!=EOF){
while(T--){
scanf("%d",&n);
long long ans=;
int tot=,f=;
scanf("%s",s+);
for(int i=;i<=n;++i) scanf("%lld",v+i);
for(int i=n;i>=;--i) if(s[i]=='(') p[++tot]=i;
for(int i=;i<=n;++i) sum[i]=sum[i-]+(s[i]==')'?v[i]:);
memset(nxt,,sizeof(nxt));
for(int i=;i<=n;++i) if(s[i]==')') {
if(f) nxt[f]=i;
f=i;
}
for(int i=;i<=n;++i) mx[][i]=mx[][i]=;
mx[][]=mx[][]=-(1LL<<);
int cur=;
for(int i=;i<=tot;++i) {
for(int j=p[i]+;j<=n;++j) if(s[j]==')') mx[cur][j]=v[p[i]]*(sum[j]-sum[p[i]])+mx[cur^][j];
for(int j=n;j>=p[i];--j) if(s[j]==')') mx[cur][j]=max(mx[cur][nxt[j]],mx[cur][j]);
for(int j=p[i]-;j>=;--j) if(s[j]==')') mx[cur][j]=max(mx[cur^][j],mx[cur][nxt[j]]);
for(int j=;j<=n;++j) if(s[j]==')') ans=max(ans,mx[cur][j]);
cur^=;
}
printf("%lld\n",ans);
}
}
}
D:Sequence Swapping的更多相关文章
- 第15届浙江省赛 D Sequence Swapping(dp)
Sequence Swapping Time Limit: 1 Second Memory Limit: 65536 KB BaoBao has just found a strange s ...
- ZOJ 4027 Sequence Swapping(DP)题解
题意:一串括号,每个括号代表一个值,当有相邻括号组成()时,可以交换他们两个并得到他们值的乘积,问你最大能得到多少 思路:DP题,注定想得掉头发. 显然一个左括号( 的最远交换距离由他右边的左括号的最 ...
- zoj4027 Sequence Swapping
首先容易想到二维方程dp(i,j),表示第i个左括号去匹配到第j个右括号时产生的最大值,但如果如此表示的话,首先需要枚举(i,j)以及一个k即dp(i-1,k). 考虑变化dp(i,j)的表示方法,可 ...
- ZOJ4027 Sequence Swapping DP
link:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4027 题意: 有一个括号序列,每个括号对应一个值,现在可以使得相 ...
- 2018浙江省赛(ACM) The 15th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple
我是铁牌选手 这次比赛非常得爆炸,可以说体验极差,是这辈子自己最脑残的事情之一. 天时,地利,人和一样没有,而且自己早早地就想好了甩锅的套路. 按理说不开K就不会这么惨了啊,而且自己也是毒,不知道段错 ...
- [POJ 1674] Sorting by Swapping
Sorting by Swapping Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9514 Accepted: 50 ...
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
随机推荐
- 自定义fastjson对枚举类型的序列化及反序列化过程
通常,fastjson在序列化及反序列化枚举时,一般以下几种策略: 1).根据枚举的name值序列化及反序列化(默认) 2).根据枚举的ordinal序列化及反序列化 3).根据枚举的toString ...
- FileZilla更新服务器文件后浏览器没有刷新的一种常见情况
一.问题描述 在使用FileZilla更新服务器文件时,常出现的一种情况是: 刷新浏览器,发现该网页并未更新.但是检查网页源代码可以发现文件已经更新了,这就奇怪了,是服务器出了问题吗?还是FileZi ...
- Lucene的FuzzyQuery中用到的Levenshtein Distance(LD)算法
2019独角兽企业重金招聘Python工程师标准>>> Lucene的FuzzyQuery中用到的Levenshtein Distance(LD)算法 博客分类: java 搜索引擎 ...
- CF思维联系–CodeForces-217C C. Formurosa(这题鸽了)
ACM思维题训练集合 The Bytelandian Institute for Biological Research (BIBR) is investigating the properties ...
- C :uthash
参考: [1] uthash | 学步园 [2] 源码 [3] 官方文档 [4] [5] 一.哈希表的概念及作用 在一般的线性表或者树中,我们所储存的值写它的存储位置的关系是随机的.因此,在查找过程中 ...
- Course Selection System ZOJ - 3956 01背包+思维
Course Selection System ZOJ - 3956 这个题目居然是一个01背包,我觉得好难想啊,根本就没有想到. 这个题目把题目给的转化为 ans = a*a-a*b-b*b 这个 ...
- dp 20190617
A. Alternative Thinking 这个标的是dp,但是我感觉就只能算思维题,不是特别难, 你仔细想想就知道,你先求出01这样子满足条件的个数,如果要进行改变,最多只可以增加两个,也可以增 ...
- Dynamics 9.0 安装好后 公告出现 提示:出现错误。 请稍等片刻,然后重试。 如果问题仍然存在,请与管理员联系。
此问题为系统的Bug,示例图如下: 解决方案为修改存储过程 p_RetrievePosts,将startDate参数的默认值改成 1900-01-01,endDate参数的默认值改成 9999-12- ...
- LTE网络概述
LTE主要由两部分组成:无线接入技术演进(E-UTRAN)+系统架构演进(SAE):其中,SAE主要含有的是演进型分组交换核心网(EPC),其控制处理部分为移动性管理实体(MME),数据承载部分称为业 ...
- Polar码快速入门
Polar码快速入门 本科生在学习极化码时,并不是件简单的事情.网上极化码的资料很少,而且基本上都是较难的论文.这篇文章是用来帮你快速入门极化码. Poalr码背景 2015 年,国际电信联盟无线通信 ...