Codeforces Round #306 (Div. 2) E. Brackets in Implications 构造
2015-06-05 03:29
495 查看
E. Brackets in Implications
Time Limit: 20 SecMemory Limit: 256 MB
题目连接
http://codeforces.com/contest/550/problem/EDescription
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.
[b]Input[/b]
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.
[b]Output[/b]
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.
[b]Sample Input[/b]
4
0 1 1 0
Sample Output
YES(((0)->1)->(1->0))
HINT
[b]题意[/b]构造出一个等式,按着上面的规律,使得答案为0
[b]题解:[/b]
最后一位肯定得为0
只有2个0也无解
然后只用管3个0的情况
然后构造出a1->a2->ak->(0->(b1->(b2->....->(bk->0))))->0
然后就完了 = =
只用管到最近的三个0
[b]代码:[/b]
//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\n0\n"); else printf("NO\n"); return 0; } if( n==2 ){ if( num[0]=='1' && num[1]=='0' ) printf("YES\n1->0\n"); else printf("NO\n"); return 0; } if( num[n-1]=='1' ){ printf("NO\n"); return 0; } if( num[n-2]=='1' ){ printf("YES\n"); for(int i=0;i<n-1;++i) printf("%c->",num[i]); printf("%c",num[n-1]); printf("\n"); 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\n"); return 0; } printf("YES\n"); 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\n",num[n-1]); }
相关文章推荐
- Android开发记录20-获取缓存大小和清除缓存功能
- Jquery on() load event on a single element
- vsftpd之虚拟用户相关
- Codeforces Round #306 (Div. 2) D. Regular Bridge 构造
- Codeforces Round #306 (Div. 2) C. Divisibility by Eight 暴力
- Codeforces Round #306 (Div. 2) C
- Codeforces Round #306 (Div. 2) B. Preparing Olympiad dfs
- Codeforces Round #306 (Div. 2) B
- Codeforces Round #306 (Div. 2) A. Two Substrings 水题
- Java课程实验报告 实验三 敏捷开发与XP实践
- Codeforces Round #306 (Div. 2) A
- 线索二叉树
- 二叉树链式结构
- 二叉树顺序结构实现
- 模式匹配
- 串
- 链队列
- 顺序队列
- 斐波那契的递归函数
- 链栈