XJTU Summer Holiday Test 1(Brackets in Implications-构造)

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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-14 07:41:11

XJTU Summer Holiday Test 1(Brackets in Implications-构造)的相关文章

codeforces #550E Brackets in Implications 构造

题目大意:定义在集合{0,1}上的运算符"→",定义如下: 0→0=1 0→1=1 1→0=0 1→1=1 现在给定一个表达式a1→a2→a3→...→an,要求添加一些括号使得值为0 由于0=1→0,因此显然末尾必须是0,否则无解 然后我们这么构造: (a1→(a2→(a3→(...))))→an 由于an=0,所以前面的那些东西必须等于1 然后我们讨论an?1 如果an?1=1,那么前面那坨东西显然是1(因为0要求末尾是0) 如果an?1=0,那么找到前面第一个0,一直合成到这个0

Codeforces550E:Brackets in Implications

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 resul

CodeForces 550E Brackets in Implications(构造)

[题目链接]:click here~~ [题目大意]给定一个逻辑运算符号a->b:当前仅当a为1b为0值为0,其余为1,构造括号.改变运算优先级使得最后结果为0 [解题思路]: todo~~ /* 思路: 1.假设最后一位是1,不管怎样结果不会为0.puts("NO"); 2.那么有解的情况下最后一位必为0 2.1.进一步发现,事实上倒数第二位必为1,仅仅有1前面的结果和该位1结合才干等于1,进一步1->0=0; 2.2.假设1前面是0.那么合并这两位数,组成1,递推2.1

XJTU Summer Holiday Test 1(Divisibility by Eight-8的倍数)

C - Divisibility by Eight Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain le

「日常训练」Brackets in Implications(Codeforces Round 306 Div.2 E)

题意与分析 稍微复杂一些的思维题.反正这场全是思维题,就一道暴力水题(B). 代码 #include <bits/stdc++.h> #define MP make_pair #define PB emplace_back #define fi first #define se second #define ZERO(x) memset((x), 0, sizeof(x)) #define ALL(x) (x).begin(),(x).end() #define rep(i, a, b) fo

Codeforces Round #306 (Div. 2) (ABCE题解)

比赛链接:http://codeforces.com/contest/550 A. Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes You are given string s. Your task is to determine if the given string s contains two non-overlapping substrings "AB" and &

Codeforces Round #306 (Div. 2) D.E. 解题报告

D题:Regular Bridge 乱搞.构造 这题乱搞一下就行了.构造一个有桥而且每个点的度数都为k的无向图.方法很多,也不好叙述.. 代码如下: #include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <stack> #include <map> #include <algorithm> #define INF 0x

Codeforces Round #306 (Div. 2) (构造)

A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全,最后只能很蠢的暴力,把所有的AB和BA的位置求出来,能有一对AB和BA不重叠即可. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 char a[100005]; 5 vector<int> ab; 6 vector<i

CF149D. Coloring Brackets[区间DP !]

不知道为什么居中了,先把代码放这 #include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int N=705,MOD=1e9+7; char s[N]; long long n,f[N][N][5][5]; int st[N],top=0,m[N]; void match(){ for(int i=1;i<=