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;
    }
    
    
  • 相关阅读:
    Office办公软件停止工作解决方案
    Jquery blockUI用法
    IE浏览器对js代码的高要求
    IIS中应用程序切换物理路径遇到的问题
    using关键字
    剑指offer-面试题23-链表中环的入口节点-双指针
    剑指offer-面试题22-链表中倒数第k个节点-双指针
    剑指offer-面试题21-调整数组顺序使奇数位于偶数前面-双指针
    剑指offer-面试题20-表示数值的字符串-字符串
    剑指offer-面试题19-正则表达式匹配-字符串
  • 原文地址:https://www.cnblogs.com/Zfio/p/12912199.html
Copyright © 2011-2022 走看看