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 ...
随机推荐
- hdu_2391 Filthy Rich DP
Filthy Rich Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- Ubuntu下访问Windows中Postgresql
因为项目的原因,需要将Ubuntu中的一些信息记录到Windows中的Postgresql数据库中,查看网上信息,最后成功了,特地记录以下,需要以下步骤: (1)在Windows中Postgresql ...
- 开启Apache服务出现的错误
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for Se ...
- 使用mvp+rxjava+retrofit加载数据
将mvp和rxjava和retrofit简单整合的列子,让activity的代码不再那么臃肿,只负责显示数据. 关于mvp的介绍可以看这里, 关于Rxjava的介绍可以看这里; 关于retrofit的 ...
- Vim Install-Could not get lock......
在Ubuntu下安装Vim编辑器时,出现错误: 可能原因: 上次安装或更新没有正常完成,导致资源被锁. 解决方案: 删除异常资源,重新下载: 注意 删除异常资源时,一定要切换到root用户,否则操作无 ...
- 基于UDP的客户端和服务器端的代码设计
实验平台 linux 实验内容 编写UDP服务器和客户端程序,客户端发送消息,服务器接收消息,并打印客户端的IP地址和端口号. 实验原理 UDP是无需连接的通信,其主要实现过程如下: 同样,我们可以按 ...
- shell之路 shell核心语法【第三篇】运算
Bash 支持很多运算符,包括算数运算符.关系运算符.布尔运算符.字符串运算符和文件测试运算符. 原生bash不支持简单的数学运算,默认都是字符串操作,但是可以通过其他命令来实现 算数运算 expr. ...
- thinkphp历史漏洞
https://github.com/pochubs/pochubs/blob/master/ThinkPHP.md tp 历史漏洞 路由控制類RCE/think/App.php if (!preg_ ...
- thinkphp操作phpexcel问题
一.thinkphp引入PHPExcel到/Thinkphp/Library/Vendor/ 二.在控制器中引用 public function get_detail() { Vendor(" ...
- nginx反向代理做负载均衡以及使用redis实现session共享配置详解
1.为什么要用nginx做负载均衡? 首先我们要知道用单机tomcat做的网站,比较理想的状态下能够承受的并发访问在150到200, 按照并发访问量占总用户数的5%到10%技术,单点tomcat的用户 ...