题解:CF550E Brackets in Implications
Karieciation · · 题解
思路
奶龙构造题。
第一眼有点看不懂,考虑大分讨。
构造的边界问题最讨厌,考虑特判
分讨:
接下来就是讨论
你通过对运算规则的观察可以得出,你只能通过将整个序列分成前后两段,前段的返回值为 NO,然而当一段区间
此时我们的讨论变成了如何将 YES,不需要加括号顺序输出运算式即可,反之你发现形如 YES,设这个区间其中一个 NO。
总结一下分讨情况:
-
-
-
-
-
- 上述条件都不满足,
NO。
代码
#include <cstdio>
#define ui unsigned int
#define N 100005
#define tT template<typename T>
namespace M_K_W_ {
tT void read(T& x) {
x=0; char ch=getchar(); while(ch<'0' || ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch&15), ch=getchar();
}
ui n, a[N];
void solve() {
read(n);
for(ui i=1; i<=n; ++i)
read(a[i]);
if(n==1) {
if(a[1]) puts("NO");
else puts("YES\n0");
}
else if(n==2) {
if(a[1]==1&&a[2]==0) puts("YES\n(1->0)");
else puts("NO");
}
else {
if(a[n]==1) puts("NO");
else if(a[n-1]==1) {
puts("YES");
for(ui i=1; i<n; ++i) {
putchar(a[i]|48);
printf("->");
}
puts("0");
}
else {
bool flag=false;
ui t;
for(ui i=1; i<n-1; ++i)
if(!a[i])
flag=true, t=i;
if(flag) {
puts("YES");
for(ui i=1; i<t; ++i) {
putchar(a[i]|48);
printf("->");
}
putchar('(');
putchar(a[t]|48);
printf("->(");
for(ui i=t+1; i<n-1; ++i) {
putchar(a[i]|48);
printf("->");
}
putchar(a[n-1]|48);
puts("))->0");
}
else puts("NO");
}
}
}
}
int main() {
M_K_W_ :: solve();
return 0;
}