本文主要是介绍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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!