【CF 550E】 Brackets in Implications(贪心)

【CF 550E】 Brackets in Implications(贪心)


E. Brackets in Implications
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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:

1->1 = 1

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

0->0->0 = 1->0 = 0

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 v1v2 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.

Sample test(s)
input
4
0 1 1 0
output
YES
(((0)->1)->(1->0))
input
2
1 1
output
NO
input
1
0
output
YES
0
题目要求输出一种运算方式(加括号) 让其最后得到0

1->1 0->1 0->0均=1

1->0 = 0

可知想要得到0 必经1->0这步 因此最后一位必须为0

前一位若为1则不需括号 因为按序运算最后一步0->1 或1->1都得到1

前一位若为0 需要往前找到前一个0 若找不到则No 否则找到第一个0后 期间的1都变成1->0

例如 011100 最后一个0 要想办法把倒数第二个0变1 0->0才能得到1 因此变成(0->(1->(1->(1->0)))) 不断运算让两个0相邻 得到1 然后就变成之前的情况了


代码如下:

#include <bits/stdc++.h>
#define LL long long
#define pr pair<int,int>
#define VI vector<int>

using namespace std;
const int INF = 0x3f3f3f3f;
const double esp = 1e-8;
const int msz = 1000;

int st;
int a[100100];
int n;

bool can()
{
    //最后一位不为0 一定得不到
    if(a[n-1] != 0) return false;
    //不存在倒二位或者倒二位为1 可行且不需要括号
    if(n-2 < 0 || a[n-2] == 1) return true;

    //倒二位为0 往前找最近的0
    for(int i = n-3; i >= 0; --i)
    {
        //找到了 记录下
        if(a[i] == 0)
        {
            st = i;
            return true;
        }
    }
    
    //找不到 No
    return false;
}

int main()
{
    while(~scanf("%d",&n))
    {

        for(int i = 0; i < n; ++i) scanf("%d",&a[i]);

        st = -1;
        //能得到0
        if(can())
        {
            puts("Yes");
            bool f = 0;
            for(int i = 0; i < n; ++i)
            {
                if(f) printf("->");
                else f = 1;
                //当i进入括号区域
                if(i == st)
                {
                    printf("(%d",a[i]);
                    for(int j = i+1; j < n-1; ++j)
                    {
                        printf("->(%d",a[j]);
                    }
                    for(; i < n-1; ++i)
                        putchar(')');
                    --i;
                }else printf("%d",a[i]);
            }
            puts("");
        }else puts("No");

    }
    return 0;
}


你可能感兴趣的:(【CF 550E】 Brackets in Implications(贪心))