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 ...
随机推荐
- 3.PEP 8是什么?
PEP 8是什么? PEP 8 is a coding convention, a set of recommendation, about how to write your Python code ...
- 剑指offer---05---用栈实现队列
题意 给了两个栈去实现队列 分析 两个栈如下情况 1 2 4 3 这个时候就不能够把4插入到第二个弹出栈了否则弹出顺序出错. 所以这个时候就应该等第二个栈空了的时候再 ...
- axis2 411
返回411加个这个就行了 _operationClient.getOptions().setProperty(HTTPConstants.CHUNKED, false); 本文转自 cd1989929 ...
- centos6.5宽带拨号上网
CentOS6以后要安装rp-pppoe这个软件,centos之前的版本是adsl-setup命令安装. (1)查看是否安装 #rpm -qa|grep rp-pppoe 没有内容输出则没安装,若可以 ...
- visibility: hidden 和 display: none的区别
相同点: 两者都可以将dom元素隐藏 不同点: 1.display: none 隐藏之后不占用文档流,而visibility: hidden却会占用文档流,如果要在隐藏元素的同时获取其尺寸信息,那就可 ...
- Flutter为什么使用Dart?
老孟导读:关于Flutter为什么使用Dart?这个话题,就像PHP是世界上最好的语言一样,争论从来没有停止过,有很多说法,比如: Google是为了推广Dart,Dart是亲儿子. Flutter团 ...
- Java TCP小结
服务端: 客户端: ServerSock ...
- [CodeForces 344D Alternating Current]栈
题意:两根导线绕在一起,问能不能拉成两条平行线,只能向两端拉不能绕 思路:从左至右,对+-号分别进行配对,遇到连续的两个“+”或连续的两个“-”即可消掉,最后如果全部能消掉则能拉成平行线.拿两根线绕一 ...
- 数据库连接池Druid的介绍,配置分析对比总结
Druid的简介 Druid首先是一个数据库连接池.Druid是目前最好的数据库连接池,在功能.性能.扩展性方面,都超过其他数据库连接池,包括DBCP.C3P0.BoneCP.Proxool.JBos ...
- CSS理论:margin-left在float中的运用
源码如下: margin-left 指的是左边的外边距,为正数时,左边间距增大,div向右偏移,为负数时,左边间距减少,相反往左偏移 双飞翼 .wrap { width: 100%; margin: ...