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后,因为0→0=1,因此末尾就变成了1

如果前面没有0,那么这个数列就是111...100

稍微手玩一下可以发现这种情况下无解。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 100100
using namespace std;
int n,a[M];
int main()
{
    int i;
    cin>>n;
    for(i=1;i<=n;i++)
        scanf("%d",&a[i]);
    if(n==1)
    {
        if(a[1]==1)
            puts("NO");
        else
            puts("YES\n0");
        return 0;
    }
    if(a[n]==1)
        return puts("NO"),0;
    for(i=1;i<=n-2;i++)
        if(a[i]!=1)
            break;
    if(i==n-1&&a[n-1]==0&&a[n]==0)
        return puts("NO"),0;
    puts("YES");
    for(i=1;i<=n-2;i++)
        printf("(%d->",a[i]);
    printf("%d",a[n-1]);
    for(i=1;i<=n-2;i++)
        putchar(‘)‘);
    puts("->0");
    return 0;
}
时间: 2024-08-05 14:36:48

codeforces #550E Brackets in Implications 构造的相关文章

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

Codeforces 429B Working out bfs构造

题目链接:点击打开链接 题意:给定n*m的矩阵 有一个人a从左上角走到右下角,只能↓或→走 另一个人b从左下角走到右上角,只能↑或→走 使得2个人的路径有且仅有一个格子是相交的. 统计2个人的权值和(相交格子的权值和不计) 问最大的权值和是多少. 思路: 首先转换一下题意,也就是找一个格子与4个角落连不相交的线. 我们观察相交的那个格子,那个格子的上下左右必然对应着一个角落. (i,j)点,那么(i-1,j)必然对应左上角或右上角的其中一个角落. 这样(i,j)点的4个相邻格子各自对应一个角落(

Codeforces 346C Number Transformation II 构造

题目链接:点击打开链接 = = 990+ms卡过 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<vector> #include<set> using namespace std; #define N 100010 #define L(x) (x<<1) #define R(x) (x<<

「日常训练」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 464A No to Palindromes!(构造)

题目链接:Codeforces 464A No to Palindromes! 题目大意:给定n和m,以及一个字符串s,s不存在长度大于2的回文子串,现在要求输出一个字典比s大的字符串,并 且说同样不存在长度大于2的回文子串. 解题思路:直接去构造即可,从最后一位开始,每次只要考虑该字符是否和前两个字符相同即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std;

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 622. Optimal Number Permutation 构造

题目链接 假设始终可以找到一种状态使得值为0, 那么两个1之间需要隔n-2个数, 两个2之间需要隔n-3个数, 两个3之间隔n-4个数. 我们发现两个三可以放到两个1之间, 同理两个5放到两个3之间....这样就构造好了. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmat

codeforces #402C Searching for Graph 构造

题目大意:给定n和p,我们需要构造一张点数为n,边数为2n+p的简单无向图,满足任意一个点数为k的子图的边数不超过2k+p 逗B题-- 我们只需要把字典序最小的2n+p条边输出就行了 下面我们来证明这么做是对的 首先这个条件等价于[删掉任意k个点,都有至少2k条边被跟着删掉] 然后我们来看这样一个图: 显然这个图是我们构造的图的子图 下面我们来证明这个性质 假如我们删掉了k个点,那么: 如果删掉的k个点都是中间那一列的,由于一个点对应至少两条边,那么我们一定至少删掉了2k条边: 否则: 如果我们

codeforces #335div2 D. Lazy Student 构造

#include<bits/stdc++.h> #define REP(i,a,b) for(int i=a;i<=b;i++) #define MS0(a) memset(a,0,sizeof(a)) using namespace std; typedef long long ll; const int maxn=1000100; const int INF=1<<29; int n,m; struct Edge { int w,is; int id; friend bo