zoukankan      html  css  js  c++  java
  • 分享二合一

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

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

    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;
    }
    
    
  • 相关阅读:
    IdentityServer4身份认证授权入门-----客户端凭据、密码模式
    Linux从创建到部署ASP.NET Core项目-----使用阿里云(Centos7)
    Docker入门之快速安装和卸载使用Centos7
    SQLServer系列(二):系统函数之聚合函数
    SpringCloud-day09-Feign与Hystrix整合
    SpringCloud-day08-Hystrix断路器
    SpringCloud-day07-Feign
    SpringCloud-day06-Ribbon负载均衡
    SpringCloud-day05-服务调用Ribbon
    SpringCloud-day04-Eureka高可用集群配置
  • 原文地址:https://www.cnblogs.com/Zfio/p/12912199.html
Copyright © 2011-2022 走看看