zoukankan      html  css  js  c++  java
  • 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+88;
    char s[N];
    int n,p[N],nxt[N];
    long long sum[N],v[N];
    long long mx[2][N];
    int main(){
        int T;
        while(scanf("%d",&T)!=EOF){
        while(T--){
            scanf("%d",&n);
            long long ans=0;
            int tot=0,f=0;
            scanf("%s",s+1);
            for(int i=1;i<=n;++i) scanf("%lld",v+i);
            for(int i=n;i>=1;--i) if(s[i]=='(') p[++tot]=i;
            for(int i=1;i<=n;++i) sum[i]=sum[i-1]+(s[i]==')'?v[i]:0);
            memset(nxt,0,sizeof(nxt)); 
            for(int i=1;i<=n;++i) if(s[i]==')') {
                if(f) nxt[f]=i;
                f=i;
            }
            for(int i=1;i<=n;++i) mx[1][i]=mx[0][i]=0;
            mx[1][0]=mx[0][0]=-(1LL<<60);
            int cur=0;
            for(int i=1;i<=tot;++i) {
                for(int j=p[i]+1;j<=n;++j) if(s[j]==')') mx[cur][j]=v[p[i]]*(sum[j]-sum[p[i]])+mx[cur^1][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]-1;j>=1;--j) if(s[j]==')') mx[cur][j]=max(mx[cur^1][j],mx[cur][nxt[j]]);
                for(int j=1;j<=n;++j) if(s[j]==')') ans=max(ans,mx[cur][j]);
                cur^=1;
            }
            printf("%lld
    ",ans);
        }
        }
    }
  • 相关阅读:
    【iBoard电子学堂开发板例程】【12个 stm32 例程发布】
    【iCore双核心组合是开发板例程】【12个 verilog 中级实验例程发布】
    【新产品发布】【iHMI43 智能液晶模块 2013 版】
    【新产品发布】【EVC8001 磁耦隔离式 USB 转 RS-485】
    【iHMI43真彩液晶模块】发布新版 DEMO 软件包,版本号为 0.14
    【新产品发布】【iCore2 ARM / FPGA 双核心板】
    急性淋巴瘤化疗术后高热不退案
    产后便秘案
    药疹治案
    胃癌化疗后急救案
  • 原文地址:https://www.cnblogs.com/mfys/p/8973477.html
Copyright © 2011-2022 走看看