Codeforces Round #306 (Div. 2) E. Brackets in Implications 构造
E. Brackets in Implications
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/550/problem/E
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 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
Sample Output
YES
(((0)->1)->(1->0))
HINT
题意
构造出一个等式,按着上面的规律,使得答案为0
题解:
最后一位肯定得为0
只有2个0也无解
然后只用管3个0的情况
然后构造出a1->a2->ak->(0->(b1->(b2->....->(bk->0))))->0
然后就完了 = =
只用管到最近的三个0
代码:
//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[];
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** char num[]; int main(){
int n;
scanf(" %d",&n);
for(int i=;i<n;++i)
scanf(" %c",&num[i]);
if( n== ){
if( num[]=='' ) printf("YES\n0\n");
else printf("NO\n");
return ;
}
if( n== ){
if( num[]=='' && num[]=='' )
printf("YES\n1->0\n");
else printf("NO\n");
return ;
}
if( num[n-]=='' ){
printf("NO\n");
return ;
}
if( num[n-]=='' ){
printf("YES\n");
for(int i=;i<n-;++i)
printf("%c->",num[i]);
printf("%c",num[n-]);
printf("\n");
return ;
}
int cnt = , from = - , to = n-;
for(int i=n-;i>= && from==-;--i)
if( num[i]=='' )
from = i;
if( from==- ){
printf("NO\n");
return ;
}
printf("YES\n");
for(int i=;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-]);
}
Codeforces Round #306 (Div. 2) E. Brackets in Implications 构造的更多相关文章
- 数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight
题目传送门 /* 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 */ #include <cstdio> #include <a ...
- DFS Codeforces Round #306 (Div. 2) B. Preparing Olympiad
题目传送门 /* DFS: 排序后一个一个出发往后找,找到>r为止,比赛写了return : */ #include <cstdio> #include <iostream&g ...
- 水题 Codeforces Round #306 (Div. 2) A. Two Substrings
题目传送门 /* 水题:遍历一边先找AB,再BA,再遍历一边先找BA,再AB,两种情况满足一种就YES */ #include <cstdio> #include <iostream ...
- Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)
题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...
- 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 ...
- 「日常训练」Brackets in Implications(Codeforces Round 306 Div.2 E)
题意与分析 稍微复杂一些的思维题.反正这场全是思维题,就一道暴力水题(B).题解直接去看官方的,很详尽. 代码 #include <bits/stdc++.h> #define MP ma ...
- Codeforces Round #306 (Div. 2) D. Regular Bridge 构造
D. Regular Bridge Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550/pro ...
随机推荐
- Code Hard or Go Home
介绍Webkit的渊源 http://hypercritical.co/2013/04/12/code-hard-or-go-home
- Python在centos下的安装
1.wget http://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz默认下载到主目录下 2.tar xzf Python-2.6.6.tgz 3 ...
- 句柄(handle)
< Back 句柄,在windows编程中用来标识: *.模块(module) *.任务(task) *.实例(instance) *.文件(file) *.内存块(block of memor ...
- Page 63-64 Exercises 2.3.7 -------Introduction to Software Testing (Paul Ammann and Jeff Offutt)
Use the following method printPrimes() for question a-d below //Find and prints n prime integers pri ...
- ps做gif 登陆下拉菜单效果
作者这里仅介绍登录动画的制作思路和简单过程.一些细节的制作,如登录框,每一帧的图像等都需要自己根据参考图慢慢完成.最终效果 1.新建大小适当的文件,背景填充暗蓝色.首先设计一个底座,主要用图层样式来完 ...
- MemoryMappingFile泄漏分析过程
最近项目突然收到了一个紧急的问题报告 - 用户在进行某些关键操作的时候整个软件突然就crash掉了.幸好产品继承了自动抓取dump的功能... 收到dump之后,通过windbg打开,查看相应的c ...
- 【转】Struts2中的MethodFilterInterceptor(转)
这是一个Struts2.1.8.1应用,代码如下 首先是web.xml文件 view plaincopy to clipboardprint?01.<?xml version="1.0 ...
- 《Java数据结构与算法》笔记-CH4-4循环队列
/** * 循环队列 */ class Queue { private int maxSize; private long[] queue; private int front; private in ...
- mysql kill操作
KILL语法 KILL [CONNECTION | QUERY] thread_id 每个与mysqld的连接都在一个独立的线程里运行,您可以使用SHOW PROCESSLIST语句查看哪些线程正在运 ...
- scp,ssh双机互信操作步骤
Node1:# ssh-keygen -t rsa 这一步是生成密钥# ssh-copy-id -i ~/.ssh/id_rsa.pub root@node2.xueping365.com ~/.s ...