Codeforces Round #653 (Div. 3) C. Move Brackets（思维）

Move Brackets：

() 是对的

(()) 也是对的

(()()) 也是对的

)( 是错的

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main() {
int t;
cin >> t;
while(t--) {
int n;
int vis[1000];
memset(vis, 0, sizeof(vis));
cin >> n;
char s[1000];

for(int i = 0; i < n; i++) {	//将'('')'用数字表示；('-')
char c;
cin >> c;
if(c == '(') c = '1';
if(c == ')') c = '0';
s[i] = c;
}

int m = n / 2;	//m表示总对数

for(int i = n - 1; i >= 1; i--) {
if(s[i] == '1') continue;
for(int j = i - 1; j >= 0; j--) {	//查找这个')'之前有没有'('
if(vis[j] == 0 && s[j] == '1') {
m--;
vis[j]++;
vis[i]++;
break;
}
}
if(vis[i]) continue; //[i]没有被标记，则说明它之前没有'('了，剩下的还没有匹配对就是需要移动的步数；
else break;
}

cout << m << endl;
}

return 0;
}

原题：

You are given a bracket sequence s of length n, where n is even (divisible by two). The string s consists of n/2 opening brackets '(' and n/2 closing brackets ')'.

In one move, you can choose exactly one bracket and move it to the beginning of the string or to the end of the string (i.e. you choose some index i, remove the i-th character of ss and insert it before or after all remaining characters of s).

Your task is to find the minimum number of moves required to obtain regular bracket sequence from s. It can be proved that the answer always exists under the given constraints.

Recall what the regular bracket sequence is:

• "()" is regular bracket sequence;
• if s is regular bracket sequence then "(" + s + ")" is regular bracket sequence;
• if s and t are regular bracket sequences then s + t is regular bracket sequence.

For example, "()()", "(())()", "(())" and "()" are regular bracket sequences, but ")(", "()(" and ")))" are not.

You have to answer t independent test cases.

The first line of the input contains one integer t (1≤t≤2000) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (2≤n≤50) — the length of ss. It is guaranteed that n is even. The second line of the test case containg the string s consisting of n/2 opening and n/2 closing brackets.

For each test case, print the answer — the minimum number of moves required to obtain regular bracket sequence from s. It can be proved that the answer always exists under the given constraints.

Input：        Output：

4
2
)( ------------------ 1
4
()() ---------------- 0
8
())()()( ------------ 1
10
)))((((()) ---------- 3

Note

In the first test case of the example, it is sufficient to move the first bracket to the end of the string.

In the third test case of the example, it is sufficient to move the last bracket to the beginning of the string.

In the fourth test case of the example, we can choose last three openning brackets, move them to the beginning of the string and obtain "((()))(())".

07-02 49

06-29 94

06-29 164

06-29 283

06-29 106

06-29 78

06-29 43

06-29 276

07-08 54

06-29 1530

Codeforces Round #653 (Div. 3)(A 二分 B 水 C 水 D 水 E1 思维 F 模拟 得到有序数组 )

©️2020 CSDN 皮肤主题: 黑客帝国 设计师: 上身试试

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。