• 【codeforces 550E】Brackets in Implications


    time limit per test2 seconds
    memory limit per test256 megabytes
    inputstandard input
    outputstandard output
    Implication is a function of two logical arguments, its value is false if and only if the value of the first argument is true and the value of the second argument is false.

    Implication is written by using character ”, and the arguments and the result of the implication are written as ‘0’ (false) and ‘1’ (true). According to the definition of the implication:

    When a logical expression contains multiple implications, then when there are no brackets, it will be calculated from left to fight. For example,

    .

    When there are brackets, we first calculate the expression in brackets. For example,

    .

    For the given logical expression determine if it is possible to place there brackets so that the value of a logical expression is false. If it is possible, your task is to find such an arrangement of brackets.

    Input
    The first line contains integer n (1 ≤ n ≤ 100 000) — the number of arguments in a logical expression.

    The second line contains n numbers a1, a2, …, an (), which means the values of arguments in the expression in the order they occur.

    Output
    Print “NO” (without the quotes), if it is impossible to place brackets in the expression so that its value was equal to 0.

    Otherwise, print “YES” in the first line and the logical expression with the required arrangement of brackets in the second line.

    The expression should only contain characters ‘0’, ‘1’, ‘-’ (character with ASCII code 45), ‘>’ (character with ASCII code 62), ‘(’ and ‘)’. Characters ‘-’ and ‘>’ can occur in an expression only paired like that: (“->”) and represent implication. The total number of logical arguments (i.e. digits ‘0’ and ‘1’) in the expression must be equal to n. The order in which the digits follow in the expression from left to right must coincide with a1, a2, …, an.

    The expression should be correct. More formally, a correct expression is determined as follows:

    Expressions “0”, “1” (without the quotes) are correct.
    If v1, v2 are correct, then v1->v2 is a correct expression.
    If v is a correct expression, then (v) is a correct expression.
    The total number of characters in the resulting expression mustn’t exceed 106.

    If there are multiple possible answers, you are allowed to print any of them.

    Examples
    input
    4
    0 1 1 0
    output
    YES
    (((0)->1)->(1->0))
    input
    2
    1 1
    output
    NO
    input
    1
    0
    output
    YES
    0

    【题目链接】:http://codeforces.com/contest/550/problem/E

    【题解】

    如果是这样的
    xxxxxxxxxxxxxxxxxxxx10
    可以发现不管怎么样最后的答案都是0;
    因为倒数第二个数字的1保证了前n-1个数字按顺序一定是1(0或1和1都是1)然后再和0混合一下就是0了;
    假设倒数第二个数字不是1而是0
    xxxxxxxxxxxxxxxxxxxxxxxx00
    那么就在前面再找一个0
    比如
    xxxxxxxxxxxxxxxx0xxxxxx00
    把那个0周围的按照下面处理一下
    xxxxxxxxxxxxxxx(0(xxxxxx0))0
    然后因为第1位是0的时候第二位是什么结果都是1

    那个括号嵌套里的东西最终都会变成1

    xxxxxxxxxxxxxxx10
    这样就变成肯定有解的情况了;
    特判一下无解的那些情况就好

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int MAXN = 1e5+100;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    int n,a[MAXN];
    
    void wujie()
    {
        puts("NO");
        exit(0);
    }
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        rei(n);
        rep1(i,1,n)
            rei(a[i]);
        if (a[n]!=0)
            wujie();
        if (n==1)
        {
            puts("YES");
            printf("%d
    ",a[n]);
            return 0;
        }
        if (a[n-1]==1)
        {
            puts("YES");
            rep1(i,1,n)
                {
                    printf("%d",a[i]);
                    if (i==n)
                        puts("");
                    else
                        printf("->");
                }
            return 0;
        }
        if (n==2)
            wujie();
        int pos = n-2;
        while (pos>=1 && a[pos]==1) pos--;
        if (pos==0) // 111100
            wujie();
        else
        {
            puts("YES");
            rep1(i,1,pos-1)
                printf("%d->",a[i]);
            printf("(0->(");
            rep1(i,pos+1,n-2)
                printf("%d->",a[i]);
            printf("0))->0");
            puts("");
        }
        return 0;
    }
    
  • 相关阅读:
    C#读取Excel设置(亲测可用)
    vue element-ui的对话框dialog没有height怎么解决?
    sqlserver不同服务器的不同数据库如何复制
    es6-对象与数组的解构赋值
    win10电脑上不了网了
    sqlserver数据库备份之后再还原
    “相对路径”以及“绝对路径”使用之坑
    sqlserver表-添加大量测试数据
    vue文件命名规范
    Git大小写问题
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626781.html
Copyright © 2020-2023  润新知