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

本文介绍了一种通过优化括号序列来获得最大分数的方法。玩家需要在满足特定条件的情况下交换括号位置,以达到最高得分。文章详细解释了算法思路,并提供了一段C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Sequence Swapping

Time Limit: 1 Second      Memory Limit: 65536 KB

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.

题解:
一对括号交换相当于左括号向右移一位,右括号想左移一位,
所有交换中所有左括号的相对位置不变,右括号同理。
可以从右边开始枚举每个左括号移动的位置,左括号移动
到一个点的加分等于此括号到这个点的加分加上之前的括号
所能到达的点的最大得分。
代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=1003;
char t[maxn];
ll a[maxn],dp[maxn][maxn];
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        memset(dp,0,sizeof(dp));
        int n;scanf("%d",&n);
        scanf("%s",t+1);
        for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
        int now=0;
        ll ma=0;
        for(int i=n;i>=1;i--)
        {
            if(t[i]=='(')
            {
                now++;
                for(int j=i+1;j<=n;j++)
                {
                    if(t[j]=='(')dp[now][j]=dp[now][j-1];
                    else dp[now][j]=a[i]*a[j]+dp[now][j-1];
                    ma=max(dp[now][j]+dp[now-1][j],ma);
                }
                for(int j=i;j<=n;j++)dp[now][j]+=dp[now-1][j];
                for(int j=n;j>1;j--)dp[now][j-1]=max(dp[now][j-1],dp[now][j]);
            }
        }
        printf("%lld\n",ma);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值