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 ...
随机推荐
- ngrepeat 时注意的地方和一些little tricks
angularjs的一些使用经验总结,此篇文章单谈ng指令之一ngrepeat 1. ngrepeat 时报错 Duplicates in a repeater are not allowed, 正常 ...
- zepto.js 自定义打包集成其他模块构建流程
1.首先在自己的电脑上要安装Node.js和npm包管理工具: 2.从github上下载zepto.js的源文件包到本地磁盘(例如:E:\Learning\JS): 地址:https://github ...
- Linux内核中的中断栈与内核栈的补充说明【转】
转自:http://blog.chinaunix.net/uid-12461657-id-3487463.html 原文地址:Linux内核中的中断栈与内核栈的补充说明 作者:MagicBoy2010 ...
- 【原创】Linux环境下的图形系统和AMD R600显卡编程(8)——AMD显卡DRM驱动初始化过程
前面几个blog对DRM驱动.显卡的显存管理机制.中断机制都进行了一些描述,现在阅读AMD drm驱动的初始化过程应该会轻松许多. 下面是一AMD的开发人员编写的文章(先暂时放在这里,后续有时间再添加 ...
- linux 内核库函数 【转】
转自:http://blog.chinaunix.net/uid-20321537-id-1966892.html 当编写驱动程序时,一般情况下不能使用C标准库的函数.Linux内核也提供了与标准库函 ...
- python实现websocket
# websocket实现原理 ''' 1.服务端开启socket,监听ip和端口 2.客户端发送连接请求(带上ip和端口) 3.服务端允许连接 4.客户端生成一个随机字符串,和magic strin ...
- 达梦数据库CAST与ROUND函数
https://blog.csdn.net/zry1266/article/details/50856260
- 去除整站下载文件中的tppabs等冗余代码
用TeleprotUltra复制了一个网站,结果网页中出现了很多形如tppabs=””的冗余代码,点击vs中的“在文件中查找”图标,打开“查找和替换”对话框,转到“快速替换”,然后进行以下设置: “查 ...
- 计蒜客 28319.Interesting Integers-类似斐波那契数列-递推思维题 (Benelux Algorithm Programming Contest 2014 Final ACM-ICPC Asia Training League 暑假第一阶段第二场 I)
I. Interesting Integers 传送门 应该是叫思维题吧,反正敲一下脑壳才知道自己哪里写错了.要敢于暴力. 这个题的题意就是给你一个数,让你逆推出递推的最开始的两个数(假设一开始的两个 ...
- jenkins新手入门教程
Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能. 虽然jenkins提供了Window.Lin ...