E. Brackets in Implications
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/550/problem/E
Description
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 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.
Sample Input
4
0 1 1 0
Sample Output
YES
(((0)->1)->(1->0))
HINT
题意
构造出一个等式,按着上面的规律,使得答案为0
题解:
最后一位肯定得为0
只有2个0也无解
然后只用管3个0的情况
然后构造出a1->a2->ak->(0->(b1->(b2->....->(bk->0))))->0
然后就完了 = =
只用管到最近的三个0
代码:
//qscqesze #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) #define maxn 2000001 #define mod 10007 #define eps 1e-9 int Num; char CH[20]; const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } inline void P(int x) { Num=0;if(!x){putchar('0');puts("");return;} while(x>0)CH[++Num]=x%10,x/=10; while(Num)putchar(CH[Num--]+48); puts(""); } //************************************************************************************** char num[100004]; int main(){ int n; scanf(" %d",&n); for(int i=0;i<n;++i) scanf(" %c",&num[i]); if( n==1 ){ if( num[0]=='0' ) printf("YES 0 "); else printf("NO "); return 0; } if( n==2 ){ if( num[0]=='1' && num[1]=='0' ) printf("YES 1->0 "); else printf("NO "); return 0; } if( num[n-1]=='1' ){ printf("NO "); return 0; } if( num[n-2]=='1' ){ printf("YES "); for(int i=0;i<n-1;++i) printf("%c->",num[i]); printf("%c",num[n-1]); printf(" "); return 0; } int cnt = 0 , from = -1 , to = n-2; for(int i=n-3;i>=0 && from==-1;--i) if( num[i]=='0' ) from = i; if( from==-1 ){ printf("NO "); return 0; } printf("YES "); for(int i=0;i<from;++i) printf("%c->",num[i]); for(int i=from;i<to;++i) printf("(%c->",num[i]); printf("%c",num[to]); for(int i=from;i<to;++i) printf(")"); printf("->%c ",num[n-1]); }