B - Brackets in Implications

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d
& %I64u

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 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 Input

Input
4
0 1 1 0
Output
YES
(((0)->1)->(1->0))
Input
2
1 1
Output
NO
Input
1
0
Output
YES
0

这题要用构造法,

n=1特判

考虑n>=2的情况

不难发现最后那个数必须是0。否则无解(由于1和不论什么数左运算结果为1)

倒数第二个数若为1,则(..1)->0 =0

否则倒数第二个数为0:

此时若倒数第三个数为0 (...(0->0))->0=0 (0->0)

否则倒数第三个数为1 (...(1->0))->0 因为(..1)->0 =0  所以把1右运算 ..->(1->0)=..->0 想让结果为1,则..=0

考虑前面有1个0

(...->(0->(1->1->..->1->0))->0 = (..->(0->0))->0=(..->1)->0=1->0 =0

有解

否则前面均为1

1->1->1->1->0->0  不管怎么括都无解

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
#include<string>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define MAXN (1000000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int a[MAXN],n;
char str1[]="YES\n",str2[]="NO\n";
string logic(string s1,string s2)
{
string p=""+s1+"->"+s2+"";
p="("+p+")"; return p;
}
string itos(int x)
{
if (x) return string("1");
return string("0");
}
string logic(int i,int j)
{
string p(itos(a[i]));
Fork(k,i+1,j)
{
p+="->"+itos(a[k]);
}
p="("+p+")"; return p;
}
int main()
{
// freopen("B2.in","r",stdin);
// freopen(".out","w",stdout); cin>>n;
For(i,n) scanf("%d",&a[i]); if (a[n]==1)
{
cout<<str2;
return 0;
} if (n==1)
{
cout<<str1<<"0\n";
return 0;
} if (a[n-1]==1)
{
string p;
p=logic(logic(1,n-1),"0");
cout<<str1<<p<<endl;
return 0;
}
if (a[n-1]==0)
{
ForD(i,n-2)
if (a[i]==0)
{
string p=logic(i+1,n-1);
p=logic("0",p);
if (i>1) p=logic(logic(1,i-1),p);
p=logic(p,"0");
cout<<str1<<p<<endl;
return 0;
}
} cout<<str2; return 0;
}

最新文章

  1. 关于印发利用DEM确定耕地坡度分级技术规定(试行)的通知
  2. Purfer Sequence
  3. MATLAB中白噪声的产生
  4. Go Go
  5. 从web移动端布局到react native布局
  6. QAQ
  7. 黄聪:Discuz X2.5、3.0、3.1、3.2 如何不用插件实现用户名只允许中文注册
  8. c# List Sort排序
  9. GPIO的8种模式详解
  10. hdu5136:组合计数、dp
  11. perl 读取cookie
  12. 面向对象设计模式_命令模式(Command)解读
  13. fedora上安装ffmpeg
  14. Google API Design Guide (谷歌API设计指南)中文版
  15. 在内存中加载DLL
  16. TensorFlow基本--张量
  17. 关于COM类工厂80070005和8000401a错误分析及解决办法
  18. redis和memcached相关
  19. centos6.5 redis 安装配置及java调用
  20. 利用Python实现倒序任意整数

热门文章

  1. spring in action 学习十一:property placeholder Xml方式实现避免注入外部属性硬代码化
  2. 转:Java SoftReference 使用构建对象缓存
  3. Activity管理类
  4. shiro多realm验证之——shiro实现不同身份使用不同Realm进行验证(转)
  5. 关于js无法设置input的value的问题
  6. 让Asp.net Web预启动
  7. 程序员利用javascript代码开发捕鱼游戏
  8. string那些事之replace
  9. 51nod 1062 序列中最大的数【打表】
  10. 洛谷——P2640 神秘磁石