XJTU Summer Holiday Test 1(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 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
YES
(((0)->1)->(1->0))
2
1 1
NO
1
0
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;
}
XJTU Summer Holiday Test 1(Brackets in Implications-构造)的更多相关文章
- Codeforces Round #306 (Div. 2) E. Brackets in Implications 构造
E. Brackets in Implications Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...
- codeforces #550E Brackets in Implications 结构体
标题效果:定义集合中{0,1}\{0,1\}上的运算符"→\rightarrow",定义例如以下: 0→0=10\rightarrow 0=1 0→1=10\rightarrow ...
- CodeForces 550E Brackets in Implications 推理
给出一个四个规则 0->0=1 0->1=1 1->0=0 1->1=0 我自己当时一味的去找规律,没有把式子好好推一推. 当然每个人都能想到a[n]=0是必须的 当a[n ...
- 「日常训练」Brackets in Implications(Codeforces Round 306 Div.2 E)
题意与分析 稍微复杂一些的思维题.反正这场全是思维题,就一道暴力水题(B).题解直接去看官方的,很详尽. 代码 #include <bits/stdc++.h> #define MP ma ...
- XJTU Summer Holiday Test 1(Divisibility by Eight-8的倍数)
C - Divisibility by Eight Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- CodeForces 550E Brackets in Implications(构造)
[题目链接]:click here~~ [题目大意]给定一个逻辑运算符号a->b:当前仅当a为1b为0值为0,其余为1,构造括号.改变运算优先级使得最后结果为0 [解题思路]: todo~~ / ...
- Codeforces Round #306 (Div. 2)
A. Two Substrings You are given string s. Your task is to determine if the given string s contains t ...
- Codeforces Round #306 (Div. 2) ABCDE(构造)
A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全, ...
- Codeforces Round #306 (Div. 2) D.E. 解题报告
D题:Regular Bridge 乱搞. 构造 这题乱搞一下即可了.构造一个有桥并且每一个点的度数都为k的无向图. 方法非常多.也不好叙述.. 代码例如以下: #include <cstdio ...
随机推荐
- Java面试题之CyclicBarrier和CountDownLatch的区别
1.CyclicBarrier的某个线程运行到某个点后停止运行,直到所有线程都达到同一个点,所有线程才会重新运行: CountDownLatch线程运行到某个点后,计数值-1,该线程继续运行,直到计数 ...
- 使用fdisk命令对linux硬盘进行操作
fdisk是linux自带的硬盘分区工具,可以对硬盘进行分区,或者对硬盘分区进行调整.本次试验环境请参考[Linux磁盘系统基础知识] 首先选择要进行操作的磁盘 [root@a ~]# fdisk / ...
- Linux wget 安装JDK失败
windows 下安装的话,查看网络,就会发现,是带cookie,回调参数Authparam 验证的
- Android消息推送解决方案
前言 消息推送在Android开发中应用的场景是越来越多了,比如说电商产品进行活动宣传.资讯类产品进行新闻推送等等,如下图: 推送消息截图 本文将介绍Android中实现消息推送的7种主流解决方案 目 ...
- msvc交叉编译:使用vcvarsall.bat设置命令行编译环境
一直以来我只知道vc设置命令行编译环境的批处理命令是%VS140COMNTOOLS%/Common7/Tools下的vsvars32.bat,(%VS140COMNTOOLS%为定义vs2015公共工 ...
- MY97 日期控件只输入今天之前的值
<script type="text/javascript"> function GetDate() { ...
- Knockout 双向绑定的理解
今天做了个需求就是上传图片,然后在代码中通过jQuery给一个标签赋值,经过前台的debug,发现这个值赋值成功了,但是提交到后台的请求里就没了,然后经历了一顿度娘,结果中发现了问题. 既然knock ...
- ubuntu常用终端命令
# ctrl + l - 清屏 . cLear# ctrl + c - 终止命令. # ctrl + d - 退出 shell,好像也可以表示EOF. # ctrl + z - 将当前进程置于后台,f ...
- 利用ICSharpCode进行压缩和解压缩
说说我利用ICSharpCode进行压缩和解压缩的一些自己的一下实践过程 1:组件下载地址 参考文章:C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件 2: 文件类 // ...
- phpcms编辑器添加一键排版控件
CKEditor添加一键排版插件实例,大家都知道phpcms也是ckeditor编辑器,那么如果增加这个一键排版这个牛逼功能呢增加好了后,效果图是这样的 废话不多说,直接说步骤第一步:config.j ...