日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Unusual Competitions CodeForces - 1323C(思维)

發布時間:2023/12/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Unusual Competitions CodeForces - 1323C(思维) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

A bracketed sequence is called correct (regular) if by inserting “+” and “1” you can get a well-formed mathematical expression from it. For example, sequences “(())()”, “()” and “(()(()))” are correct, while “)(”, “(()” and “(()))(” are not.

The teacher gave Dmitry’s class a very strange task — she asked every student to come up with a sequence of arbitrary length, consisting only of opening and closing brackets. After that all the students took turns naming the sequences they had invented. When Dima’s turn came, he suddenly realized that all his classmates got the correct bracketed sequence, and whether he got the correct bracketed sequence, he did not know.

Dima suspects now that he simply missed the word “correct” in the task statement, so now he wants to save the situation by modifying his sequence slightly. More precisely, he can the arbitrary number of times (possibly zero) perform the reorder operation.

The reorder operation consists of choosing an arbitrary consecutive subsegment (substring) of the sequence and then reordering all the characters in it in an arbitrary way. Such operation takes l nanoseconds, where l is the length of the subsegment being reordered. It’s easy to see that reorder operation doesn’t change the number of opening and closing brackets. For example for “))((” he can choose the substring “)(” and do reorder “)()(” (this operation will take 2 nanoseconds).

Since Dima will soon have to answer, he wants to make his sequence correct as fast as possible. Help him to do this, or determine that it’s impossible.

Input
The first line contains a single integer n (1≤n≤106) — the length of Dima’s sequence.

The second line contains string of length n, consisting of characters “(” and “)” only.

Output
Print a single integer — the minimum number of nanoseconds to make the sequence correct or “-1” if it is impossible to do so.

Examples
Input
8
))((())(
Output
6
Input
3
(()
Output
-1
Note
In the first example we can firstly reorder the segment from first to the fourth character, replacing it with “()()”, the whole sequence will be “()()())(”. And then reorder the segment from the seventh to eighth character, replacing it with “()”. In the end the sequence will be “()()()()”, while the total time spent is 4+2=6 nanoseconds.
思路:一開始的思路是,看有多少個括號匹配,然后看匹配的括號之外的括號,再加一些判斷。結果wa到了第六個樣例。。重新整理思路,對于右括號)來說,如果沒有左括號(與之匹配的話,就說明從這一點開始就要重新排序了。這樣我們記錄一下最起始的這個位置,然后往后找直到左右括號數目匹配了,就直接排序就好了。這一段是必須要排序的。因此這么貪心不會多。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=1e6+100; int n; string s;inline void solve() {int l=0,r=0,rk=-1,ans=0;for(int i=0;i<n;i++){if(s[i]=='(') l++;else{if(l) l--;else {r++;if(rk==-1) rk=i;}}if(l==r&&l){ans+=(i-rk+1);l=0,r=0,rk=-1;}}cout<<ans<<endl; } int main() {cin>>n>>s;int num1=0,num2=0;for(int i=0;i<n;i++) {if(s[i]=='(') num1++;else num2++;}if(num1!=num2) cout<<"-1"<<endl;else solve();return 0; }

努力加油a啊,(o)/~

總結

以上是生活随笔為你收集整理的Unusual Competitions CodeForces - 1323C(思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。