Topcoder SRM 688 Div2

Source: Internet
Author: User
Tags bitset

Topcoder SRM 688 Div2
250:

#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <ctime>using namespace STD;classParenthesesdiv2easy { Public:intGetdepth (string);};intParenthesesdiv2easy::getdepth (strings) {intn = s.size ();if(n = =0)return 0;intCNT =0;intPre =0;intres =0; for(inti =0; I < n; i++) {if(S[i] = =' ) ') cnt--;Else if(S[i] = =' (') cnt++;if(CNT = =0) {res = max (res, getdepth (S.SUBSTR (pre +1, I-pre-1)) +1); Pre = i +1; }    }returnRes;} <%:testing-code%>//powered by [Kawigiedit] 2.0!

500:
/*
Thought for a while
First, the legal brackets are eliminated.
And then all that's left is an illegal parenthesis.

首先采用贪心的变法,遇到只有)没有(与它对应的时候,把)变成(变换后的就是合法情况,或者左括号多的情况左括号多的情况,则从后往前变(为),直到总情况合法分类讨论后发现,总的操作次数是满足题意的。

*/

#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <ctime>using namespace STD;classParenthesesdiv2medium { Public: vector <int>Correctstring);}; vector <int>Parenthesesdiv2medium::correct (strings) {intn = s.size ();intvis[ -]; for(inti =0; I < n; i++) Vis[i] =0; Stack<int>Sta vector<int>Res for(inti =0; I < n; i++) {if(S[i] = =' (') Sta.push (i);//, printf ("i =%d, first\n", i);        Else if(S[i] = =' ) '&&!sta.empty ()) {intU = i, V = sta.top (); Sta.pop ();//printf ("u =%d,v =%d\n", u,v);Vis[u] =1, vis[v] =1;//printf ("I =%d, second\n", i);}    } vector<int>Tres for(inti =0; I < n; i++) {if(Vis[i] = =0) Tres.push_back (i); }intCNT =0; for(inti =0; I < (int) Tres.size (); i + +) {//printf ("tres[%d] =%d\n", I, Tres[i]);        if(S[tres[i]] = =' (') cnt++;Elsecnt--;if(CNT <0) Vis[tres[i]] =1, cnt + =2, Res.push_back (Tres[i]); }if(CNT <0) { for(inti =0; I < n; i++) {if(Vis[i] = =0&& S[i] = =' ) ') Vis[i] =1, cnt + =2, Res.push_back (i);if(CNT >=0) Break; }    }if(CNT >0) { for(inti = n-1; I >=0; i--) {if(Vis[i] = =0&& S[i] = =' (') Vis[i] =1, cnt-=2, Res.push_back (i);if(CNT <=0) Break; }} sort (Res.begin (), Res.end ());//for (int i = 0; i < (int) res.size (); i++) printf ("%d", res[i]);    //puts ("");    returnRes;} <%:testing-code%>//powered by [Kawigiedit] 2.0!

1000:
/*
Divide all the strings into countless substrings according to L and R, leaving the characters that are not part of any substrings to be used as a statistical preparation for the elements in the substring substitution
Then, each substring can have two number sum1,sum2, respectively, the opening parenthesis is illegal and the right parenthesis is not valid number

注意每次替换子串中一个元素,比如(,那么sum1-=2所以子串应该替换比如(直到sum1<=2,此时若sum1为1的话,可证此时sum2也为1,表示还剩下一对不合法的(和),只要交换他们两个就可以

*/

#include <vector>#include <list>#include <map>#include <set>#include <deque>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <ctime>using namespace STD;classParenthesesdiv2hard { Public:intMinswaps (string, vector <int>, vector <int>);};Const intMAXN = -+5;intSUM1[MAXN], SUM2[MAXN], VIS[MAXN];intParenthesesdiv2hard::minswaps (stringS vector <int>L vector <int>R) {intn = s.size (), M = L.size (); for(inti =0; I <= N; i++) Vis[i] =0; for(inti =0; I < m; i++)if((R[i]-l[i])%2==0)return-1; for(inti =0; I < m; i++) {intCNT =0; Sum1[i] = Sum2[i] =0; for(intj = L[i]; J <= R[i]; J + +) {if(S[j] = =' (') cnt++;Elsecnt--;if(CNT <0) cnt++, sum2[i]++; VIS[J] =1; } Sum1[i] = max (0, CNT); }intC1, C2; C1 = C2 =0; for(inti =0; I < n; i++) {if(Vis[i] = =1)Continue;if(S[i] = =' (') c1++;Elsec2++; }intS1, S2; S1 = S2 =0;intres =0; for(inti =0; I < n; i++) {S1 + = Sum1[i]/2; S2 + = Sum2[i]/2;if(Sum1[i]%2) res++; }//printf ("S1 =%d, S2 =%d, C1 =%d, C2 =%d\n", s1, S2, C1, C2);    if(S1 > S2 && S1 > s2 + c2)return-1;Else if(S2 > S1 && s2 > S1 + C1)return-1;Else returnRes + max (S1, S2);} <%:testing-code%>//powered by [Kawigiedit] 2.0!

Topcoder SRM 688 Div2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.