CodeForces - 1504C Balance the Bits(思维+构造)
题目链接:https://vjudge.net/problem/CodeForces-1504C
题目大意:给出一个长度为 nnn 的 010101 串,现在要求构造出两个长度为 nnn 的合法括号序列,使得在 010101 串对应为 000 的位置,两个序列的括号不相同,反之亦然
题目分析:本以为是贪心,但后来发现其实是一个很简单的构造
首先考虑非法的情况,无论如何起止位置一定是需要匹配的,也就是说 010101 串的开头和结尾一定都是 111 才行
其次就是必须为串的长度必须是偶数才行,否则一定无法完全匹配
然后就是 010101 串中 000 和 111 的个数都必须是偶数才行,因为假如有奇数个 000 的话,就会导致两个串中左括号和右括号的个数不相等,而其余位置两个序列的括号都是相等的,综上两个串肯定不可能同时为合法的括号序列
最后就是构造了,设 111 的个数为 kkk,我们可以贪心去让前 k2\frac{k}{2}2k 个 111 放置左括号,后 k2\frac{k}{2}2k 个 111 放置右括号
然后对于 000 的位置,我们左右括号交替放置即可,可以证明这样一定是可行的
代码:
// Problem: C. Balance the Bits
// Contest: Codeforces - Codeforces Round #712 (Div. 2)
// URL: https://codeforces.com/contest/1504/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) x&-x
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f;
}
template<typename T>
inline void write(T x)
{if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
char s[N];
int main()
{#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);int w;cin>>w;while(w--) {int n;read(n);scanf("%s",s+1);int c=0;for(int i=1;i<=n;i++) {c+=s[i]=='1';}if((n&1)||c%2!=0||s[1]!='1'||s[n]!='1') {puts("NO");continue;}string ans1,ans2;int k=1;bool flag=true;for(int i=1;i<=n;i++) {if(s[i]=='1') {if(k<=c/2) {ans1+="(";ans2+="(";} else {ans1+=")";ans2+=")";}k++;} else {if(flag) {ans1+="(";ans2+=")";} else {ans1+=")";ans2+="(";}flag^=1;}}puts("YES");cout<<ans1<<endl<<ans2<<endl;}return 0;
}
CodeForces - 1504C Balance the Bits(思维+构造)相关推荐
- Educational Codeforces Round 53C(二分,思维|构造)
#include<bits/stdc++.h> using namespace std; const int N=1e6+6; int x[N],y[N]; int sx,sy,n; ch ...
- CodeForces - 618B Guess the Permutation(思维+构造)
题目链接:点击查看 题目大意:先给出一个长度为n的序列ai,这个序列是1~n全排列中的其中一种,再给出一个n*n的矩阵,maze[i][j]=val代表min(ai,aj)=val,要求我们构造出原始 ...
- CodeForces - 1450C2 Errich-Tac-Toe (Hard Version)(思维+构造)
题目链接:点击查看 题目大意:给出一个大小为 n * m 的棋盘,规定不能有三个连续的 ' X ' 或三个连续的 ' O ' ,现在可以通过一次操作将 ' X ' 改成 ' O ' 或者将 ' O ' ...
- Codeforces 1077B Disturbed People(思维题)
Codeforces 1077B Disturbed People(思维题) There is a house with nn flats situated on the main street of ...
- A/B Matrix CodeForces - 1360G(思维构造)
You are given four positive integers n, m, a, b (1≤b≤n≤50; 1≤a≤m≤50). Find any such rectangular matr ...
- Binary String Reconstruction CodeForces - 1352F(思维+构造)
For some binary string s (i.e. each character si is either '0' or '1'), all pairs of consecutive (ad ...
- Codeforces Round #624 (Div. 3) E. Construct the Binary Tree 思维 + 构造
传送门 文章目录 题意: 思路: 题意: 给你n,dn,dn,d,让你构造有nnn个点的二叉树,他们每个节点深度和为ddd. n,d≤3000n,d\le 3000n,d≤3000. 思路: 先考虑不 ...
- 【CodeForces - 798D】Mike and distribution (思维构造,贪心,黑科技)
题干: Mike has always been thinking about the harshness of social inequality. He's so obsessed with it ...
- Codeforces Round #772 (Div. 2) C. Differential Sorting(思维+构造)
题目链接 https://codeforces.com/contest/1635/problem/C 题面 题意 给你一个长度为n的数组 a[i]a[i]a[i] ,我们有一种操作让 a[x]=a[y ...
最新文章
- 深度并非一切:普林斯顿、英特尔提出ParNet,速度和准确性显著优于ResNet
- 程序员看过这篇文章 让你学会阅读源码!
- CAD绘图软件中如何查询图纸的版本是多少
- 30/100. Queue Reconstruction by Height
- 玩转GIT系列之【如何放弃本地/服务器端所做的修改】
- 资料分享 | python机器学习教程分享来袭
- 最佳适应算法和最坏适应算法_算法:好,坏和丑陋
- C# 判断一字符串是否为合法数字(正则表达式)
- Linux(debian7)操作基础(三)之PCI/PCI-E设备配置空间
- linux yun nginx,Linux - CentOS 7 通过Yum源安装 Nginx
- 3.8 - Using the Print Function
- 利用标准差剔除异常数据
- 综述 - 染色质可及性与调控表观基因组 | Chromatin accessibility and the regulatory epigenome...
- Spring Data JDBC自动生成的增删改查CRUD分页、排序SQL语句非常简洁没有多余的SQL
- 自主存取控制方法中-----------用户权限的“授权”与“收回”
- 晶圆测试Map转换(TSK/TEL/PT301)
- Web前端小白了解这些学习秘诀,你也能成为大神!
- 7-5 计算2个复数之和与之积
- go-ethereum相关
- 视频点播RTMP推流直播流媒体服务二次开发集成接口