ADDED SRM/688-U/1A-U.cpp Index: SRM/688-U/1A-U.cpp ================================================================== --- SRM/688-U/1A-U.cpp +++ SRM/688-U/1A-U.cpp @@ -0,0 +1,123 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class ParenthesesDiv1Easy { public: + vector correct(string s) + { + if(s.length()%2 == 1) + return vector(1, -1); + + vector ans; + + int d = 0; + int peak = 0, peak_i = -1; + for(int i=0; isec_d) { bot=sec_d, bot_k=k; } + } + ans.push_back(sec_s); + ans.push_back(bot_k); + reverse(s.begin()+sec_s, s.begin()+bot_k+1); + for(auto it=s.begin()+sec_s; it!=s.begin()+bot_k+1; ++it) + *it=(*it=='(' ? ')' : '('); + --i; + } else { + d += (s[i]=='(' ? +1 : -1); + if(peak10 ? vector(1, -1) : ans; + } +}; + +// BEGIN CUT HERE +#include +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const vector & Expected, const vector & Received) { + bool ok = (Expected == Received); + if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; + cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } +#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); +#define END verify_case(_, ParenthesesDiv1Easy().correct(s));} +int main(){ + +CASE(0) + string s = ")("; + int __[] = {0, 0, 1, 1 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(1) + string s = "))))))(((((("; + int __[] = {0, 5, 6, 11 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(2) + string s = "))()())()"; + int __[] = {-1 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(3) + string s = ")()((("; + int __[] = {0, 0, 3, 3, 5, 5 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(4) + string s = "()"; + vector _; +END +CASE(5) + string s = "))))()))(((()()((()()))))(())))(()()))))))())))))()()(()()(()))())((()))()((())))()())((()())(()(())))())()(()(()(()))))())))((()((())((()())(()())(()))((()()(()(()))((((((((((())((((()())()(()()())()(((((((()()())(()))(()())))))())((((())()(())(((()((()(())))(()((()())(((((()(()())(((()))()))(((()()()))()())))(())(())(()))))())(()))))()(()(()())(()())((()))()()((())(((()(((()(()()((())())())((()))))())((((())(()()()())(((((())(((())))))())()))()())())))(()()())((((()()))))())())())()())((()))(()(()))()(())))(()))()))(())((()())())())((((())))(()())())))(()((())()()())()))()))())(())((((()()((()(()())()(((()))))(()()()))())))(()())(())())(()))()())((()))((())))()((())(()))))))()(()(((()())(())(()((()((((())))((())))))()))()(()())()())))())(())))((()))()))(()()))())(()(())((()(()))((((()()())())(()))(((())()()))(((()(()()(()())()))(()()())())))(()())(()((()()(())()()((()()()())()))())((((((()((()()(((()(()(()(())(((((((((()(())((())))(())())((())()()(((()))()()(((((((())(())))))))(())()"; +int __[] = {-2}; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +/* +CASE(6) + string s = ; + int __[] = ; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +*/ +} +// END CUT HERE ADDED SRM/688-U/1B-U.cpp Index: SRM/688-U/1B-U.cpp ================================================================== --- SRM/688-U/1B-U.cpp +++ SRM/688-U/1B-U.cpp @@ -0,0 +1,197 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class ParenthesesDiv1Medium { public: + int minSwaps(string s, vector L, vector R) + { + set> LR; + for(int i=0; i + deque> events; + for(auto lr: LR) { + events.emplace_back(lr.first, true); + events.emplace_back(lr.second, false); + } + sort(events.begin(), events.end()); + + vector req; + + stack stk; + stk.push(""); + for(int i=0; i<=s.size(); ++i) { + while(!events.empty() && events[0].first==i) { + if(events[0].second) { + stk.push(""); + } else { + if(!stk.top().empty()) + req.push_back(stk.top()); + stk.pop(); + } + events.pop_front(); + } + if(i& require, const string& remaining) + { + //cerr << "[" << remaining << "]" << endl; + + int cost = 0; + + vector sv; + for(auto r: require) + { + int sr = sig(r)/2; + sv.push_back(sr); + + //cerr << ":: [" << r << "] @ " << sr << endl; + + int d = 0; + for(char& c: r) + { + if(sr>0 && d>0 && c=='(') c=')', --sr; + else if(sr<0 && c==')') c='(', ++sr; + d += (c=='(' ? +1 : -1); + } + cost += cost_single(r); + } + + int ss = accumulate(sv.begin(), sv.end(), 0); + if(ss>0 && ss>count(remaining.begin(), remaining.end(), ')')) return -1; + if(ss<0 && -ss>count(remaining.begin(), remaining.end(), '(')) return -1; + + int nega=0, posi=0; for(int z: sv) if(z<0) nega+=-z; else posi+=z; + return cost + max(nega, posi); + } + + int cost_single(string r) + { + int cost = 0; + int d = 0; + for(char c: r) { + d += (c=='(' ? +1 : -1); + if(d==-1) ++cost, d=+1; + } + return cost; + } +}; + +// BEGIN CUT HERE +#include +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const int& Expected, const int& Received) { + bool ok = (Expected == Received); + if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; + cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } +#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); +#define END verify_case(_, ParenthesesDiv1Medium().minSwaps(s, L, R));} +int main(){ + +CASE(0) + string s = ")("; + int L_[] = {0,0,0,0}; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + int R_[] = {1,1,1,1}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = 1; +END +CASE(1) + string s = "(())"; + int L_[] = {0,2}; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + int R_[] = {1,3}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = 1; +END +CASE(2) + string s = "(((())"; + int L_[] = {0,2}; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + int R_[] = {1,3}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = 2; +END +CASE(3) + string s = "((((((((("; + int L_[] = {0,2}; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + int R_[] = {1,3}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = -1; +END +CASE(4) + string s = "()()()()"; + int L_[] = {0,0,0,0,2,2,2,4,4,6}; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + int R_[] = {1,3,5,7,3,5,7,5,7,7}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = 0; +END +CASE(5) + string s = ")()(()()((())()()()()()()))(()())()()()("; + int L_[] = {3,5,17,25,35}; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + int R_[] = {12,10,30,30,38}; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = 3; +END +/* +CASE(6) + string s = ; + int L_[] = ; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + int R_[] = ; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = ; +END +CASE(7) + string s = ; + int L_[] = ; + vector L(L_, L_+sizeof(L_)/sizeof(*L_)); + int R_[] = ; + vector R(R_, R_+sizeof(R_)/sizeof(*R_)); + int _ = ; +END +*/ +} +// END CUT HERE