• 分享二合一


    感觉今天的两道题没什么好说的。。。

    看在我还要准备文化课课件的份上就别骂我水了吧。。。

    A - Least Cost Bracket Sequence

    This is yet another problem on regular bracket sequences.

    A bracket sequence is called regular, if by inserting "+" and "1" into it we get a correct mathematical expression. For example, sequences "(())()", "()" and "(()(()))" are regular, while ")(", "(()" and "(()))(" are not. You have a pattern of a bracket sequence that consists of characters "(", ")" and "?". You have to replace each character "?" with a bracket so, that you get a regular bracket sequence.

    For each character "?" the cost of its replacement with "(" and ")" is given. Among all the possible variants your should choose the cheapest.

    Input

    The first line contains a non-empty pattern of even length, consisting of characters "(", ")" and "?". Its length doesn't exceed 5·104. Then there follow m lines, where m is the number of characters "?" in the pattern. Each line contains two integer numbers a i and b i (1 ≤ a i,  b i ≤ 106), where a i is the cost of replacing the i-th character "?" with an opening bracket, and b i — with a closing one.

    Output

    Print the cost of the optimal regular bracket sequence in the first line, and the required sequence in the second.

    Print -1, if there is no answer. If the answer is not unique, print any of them.

    Examples

    Input

    (??)
    1 2
    2 8
    

    Output

    4
    ()()
    

    简单·思路

    贪心,直接默认每个问号为右括号,然后如果左括号数量不够就向左找一个代价最小的右括号改成左括号。

    用小根堆实现。

    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <queue>
    #include <iostream>
    using namespace std;
    const int maxn=50000+10;
    #define ll long long
    char s[maxn];
    ll ans,l,r;
    struct Node{
        int x,id;
        Node(){};
        Node(int a,int b){
            x=a,id=b;
        }
        bool operator <(const Node a)const{
            return x<a.x;
        }
    };
    priority_queue<Node> q;
    
    int main(){
        scanf("%s",s);
        int len=strlen(s);
        for(int i=0;i<len;i++){
            if(s[i]=='(') {l++;continue;}
            else{
                if(s[i]=='?'){
                    int a,b;
                    scanf("%d %d",&a,&b);
                    q.push(Node(b-a,i));
                    s[i]=')';
                    ans+=b;
                }
                if(--l<0){
                    if(q.empty()){
                        printf("-1
    ");
                        return 0;
                    }
                    ans-=q.top().x;
                    s[q.top().id]='(';
                    q.pop();
                    l+=2;
                }
            }
        }
        if(l){
            printf("-1
    ");
            return 0;
        }
        printf("%lld
    %s",ans,s);
    }
    

    B - How many ways??

    老朋友矩阵乘法,没什么好说的。然而自己打的一直爆炸???

    然后上网找了个题解,发现跟自己写的一模一样。

    于是把快速幂换成他的,WA了

    于是又把main换成他的,WA了

    ???

    没办法,照着重新敲了一遍就行了。

    鬼知道我脑子怎么长得。

    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    using namespace std;
    const int N=30;
    int n,m;
    struct Mat{
        int m[N][N];
        Mat(){
            memset(m,0,sizeof(m));
        }
    };
    Mat Mul(Mat a,Mat b){
        Mat t;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                t.m[i][j]=0;
                for(int k=0;k<n;k++){
                    t.m[i][j]+=(a.m[i][k]*b.m[k][j])%1000;
                }
            }
        }
        return t;
    }
    Mat Quick_pow(Mat a,int k){
        Mat t;
        for(int i=0;i<n;i++){
            t.m[i][i]=1;
        }
        while(k){
            if(k&1){
                t=Mul(t,a);
            }
            k>>=2;
            a=Mul(a,a);
        }
        return t;
    }
    int main(){
        while(~scanf("%d%d",&n,&m) &&(n || m)){
            Mat A,B;
            while(m--){
                int  u,v;
                scanf("%d%d",&u,&v);
                A.m[u][v]=1;
                //A.m[v][u]=1;
            }
            int t;
            scanf("%d",&t);
            while(t--){
                int a,b,k;
                scanf("%d%d%d",&a,&b,&k);
                B=A;
                B=Quick_pow(A,k);
                printf("%d
    ",B.m[a][b]%1000);
            }
        }
        return 0;
    }
    
    
  • 相关阅读:
    mysql 安装教程
    Centos 7和 Centos 6开放查看端口 防火墙关闭打开
    yum源中默认好像是没有mysql的。为了解决这个问题,我们要先下载mysql的repo源。
    CentOS更改yum源与更新系统
    Linux中文显示乱码?如何设置centos显示中文
    centos 7 升级/安装 git 2.7.3
    Maven实现项目构建直接部署Web项目到Tomcat
    ODAC (odp.net) 从开发到部署
    OGNL的使用
    DotNet Core 中使用 gRPC
  • 原文地址:https://www.cnblogs.com/Zfio/p/12912199.html
Copyright © 2020-2023  润新知