cf Educational Codeforces Round 65 D. Bicolored RBS

2024-03-24 07:38

本文主要是介绍cf Educational Codeforces Round 65 D. Bicolored RBS,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原题:
D. Bicolored RBS
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A string is called bracket sequence if it does not contain any characters other than “(” and “)”. A bracket sequence is called regular (shortly, RBS) if it is possible to obtain correct arithmetic expression by inserting characters “+” and “1” into this sequence. For example, “”, “(())” and “()()” are RBS and “)(” and “(()” are not.

We can see that each opening bracket in RBS is paired with some closing bracket, and, using this fact, we can define nesting depth of the RBS as maximum number of bracket pairs, such that the 2
-nd pair lies inside the 1-st one, the 3-rd one — inside the 2-nd one and so on. For example, nesting depth of “” is 0, “()()()” is 1 and “()((())())” is 3.

Now, you are given RBS s of even length n. You should color each bracket of s into one of two colors: red or blue. Bracket sequence r, consisting only of red brackets, should be RBS, and bracket sequence, consisting only of blue brackets b, should be RBS. Any of them can be empty. You are not allowed to reorder characters in s, r or b

. No brackets can be left uncolored.

Among all possible variants you should choose one that minimizes maximum of r 's and b’s nesting depth. If there are multiple solutions you can print any of them.

Input

The first line contains an even integer n (2≤n≤2⋅10^5) — the length of RBS s.

The second line contains regular bracket sequence s (|s|=n, si∈{"(", “)”}).
Output

Print single string t of length n consisting of “0”-s and “1”-s. If ti is equal to 0 then character si belongs to RBS r, otherwise si belongs to b

.
Examples
Input

2
()

Output

11

Input

4
(())

Output
Copy

0101

Input
Copy

10
((()())())

Output
Copy

0110001111

Note

In the first example one of optimal solutions is s=
“()”. r is empty and b= “()”. The answer is max(0,1)=1

.

In the second example it’s optimal to make s=
“(())”. r=b= “()” and the answer is 1

.

In the third example we can make s=
“((()())())”. r= “()()” and b= “(()())” and the answer is 2.

中文:

给你一堆括号,然你让把这个括号染成红蓝两种颜色,定义括号的深度为一个有效括号序列嵌套的最多层数。现在问你如何将括号染色,使得两种颜色括号中深度最大值最小。

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;typedef pair<int,int> pii;
const int maxn = 200001;string s;
int n;
int d[maxn];
stack<char> sc;
int main()
{ios::sync_with_stdio(false);while(cin>>n){cin>>s;int dep=0;memset(d,0,sizeof(d));for(int i=0;i<n;i++){if(s[i]=='('){sc.push('(');d[i]=sc.size();dep=max(dep,d[i]);}else{d[i]=sc.size();sc.pop();}}for(int i=0;i<n;i++){if(d[i]<=dep/2)cout<<0;elsecout<<1;}cout<<endl;}return 0;
}

思路:

大水题
问你怎么把一组括号序列分成两组,而且两组序列深度的值最小。
先计算处原始括号序列的深度dep,那么要想使得分成两组后,两组括号序列深度最大的最小,就要使两个括号的深度尽量相同。 所以,两组括号序列深度的值就是dep/2

这篇关于cf Educational Codeforces Round 65 D. Bicolored RBS的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/840896

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

cf 164 C 费用流

给你n个任务,k个机器,n个任务的起始时间,持续时间,完成任务的获利 每个机器可以完成任何一项任务,但是同一时刻只能完成一项任务,一旦某台机器在完成某项任务时,直到任务结束,这台机器都不能去做其他任务 最后问你当获利最大时,应该安排那些机器工作,即输出方案 具体建图方法: 新建源汇S T‘ 对任务按照起始时间s按升序排序 拆点: u 向 u'连一条边 容量为 1 费用为 -c,

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

CF 508C

点击打开链接 import java.util.Arrays;import java.util.Scanner;public class Main {public static void main(String [] args){new Solve().run() ;} }class Solve{int bit[] = new int[608] ;int l

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

【CF】C. Glass Carving(二分 + 树状数组 + 优先队列 + 数组计数)

这题简直蛋疼死。。。。。 A了一下午 #include<cstdio>#include<queue>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const int maxn = 200005;int h,w,n;int C1[maxn],C2[maxn];int

【CF】E. Anya and Cubes(双向DFS)

根据题意的话每次递归分3种情况 一共最多25个数,时间复杂度为3^25,太大了 我们可以分2次求解第一次求一半的结果,也就是25/2 = 12,记录结果 之后利用剩余的一半求结果 s-结果 = 之前记录过的结果 就可以 时间复杂度降低为 3 ^ (n/2+1) 题目链接:http://codeforces.com/contest/525/problem/E #include<set