File Annotation
Not logged in
27207f6482 2016-05-12        kinaba: #include <iostream>
27207f6482 2016-05-12        kinaba: #include <sstream>
27207f6482 2016-05-12        kinaba: #include <iomanip>
27207f6482 2016-05-12        kinaba: #include <vector>
27207f6482 2016-05-12        kinaba: #include <string>
27207f6482 2016-05-12        kinaba: #include <map>
27207f6482 2016-05-12        kinaba: #include <set>
27207f6482 2016-05-12        kinaba: #include <algorithm>
27207f6482 2016-05-12        kinaba: #include <numeric>
27207f6482 2016-05-12        kinaba: #include <iterator>
27207f6482 2016-05-12        kinaba: #include <functional>
27207f6482 2016-05-12        kinaba: #include <complex>
27207f6482 2016-05-12        kinaba: #include <queue>
27207f6482 2016-05-12        kinaba: #include <stack>
27207f6482 2016-05-12        kinaba: #include <cmath>
27207f6482 2016-05-12        kinaba: #include <cassert>
27207f6482 2016-05-12        kinaba: #include <tuple>
27207f6482 2016-05-12        kinaba: using namespace std;
27207f6482 2016-05-12        kinaba: typedef long long LL;
27207f6482 2016-05-12        kinaba: typedef complex<double> CMP;
27207f6482 2016-05-12        kinaba: 
27207f6482 2016-05-12        kinaba: class ParenthesesDiv1Medium { public:
27207f6482 2016-05-12        kinaba: 	int minSwaps(string s, vector <int> L, vector <int> R)
27207f6482 2016-05-12        kinaba: 	{
27207f6482 2016-05-12        kinaba: 		set<pair<int,int>> LR;
27207f6482 2016-05-12        kinaba: 		for(int i=0; i<L.size(); ++i) {
27207f6482 2016-05-12        kinaba: 			if((R[i]-L[i]+1)%2==1)
27207f6482 2016-05-12        kinaba: 				return -1;
27207f6482 2016-05-12        kinaba: 			LR.emplace(L[i], R[i]+1);
27207f6482 2016-05-12        kinaba: 		}
27207f6482 2016-05-12        kinaba: 
27207f6482 2016-05-12        kinaba: 		// event <pos, open>
27207f6482 2016-05-12        kinaba: 		deque<pair<int,bool>> events;
27207f6482 2016-05-12        kinaba: 		for(auto lr: LR) {
27207f6482 2016-05-12        kinaba: 			events.emplace_back(lr.first, true);
27207f6482 2016-05-12        kinaba: 			events.emplace_back(lr.second, false);
27207f6482 2016-05-12        kinaba: 		}
27207f6482 2016-05-12        kinaba: 		sort(events.begin(), events.end());
27207f6482 2016-05-12        kinaba: 
27207f6482 2016-05-12        kinaba: 		vector<string> req;
27207f6482 2016-05-12        kinaba: 
27207f6482 2016-05-12        kinaba: 		stack<string> stk;
27207f6482 2016-05-12        kinaba: 		stk.push("");
27207f6482 2016-05-12        kinaba: 		for(int i=0; i<=s.size(); ++i) {
27207f6482 2016-05-12        kinaba: 			while(!events.empty() && events[0].first==i) {
27207f6482 2016-05-12        kinaba: 				if(events[0].second) {
27207f6482 2016-05-12        kinaba: 					stk.push("");
27207f6482 2016-05-12        kinaba: 				} else {
27207f6482 2016-05-12        kinaba: 					if(!stk.top().empty())
27207f6482 2016-05-12        kinaba: 						req.push_back(stk.top());
27207f6482 2016-05-12        kinaba: 					stk.pop();
27207f6482 2016-05-12        kinaba: 				}
27207f6482 2016-05-12        kinaba: 				events.pop_front();
27207f6482 2016-05-12        kinaba: 			}
27207f6482 2016-05-12        kinaba: 			if(i<s.size())
27207f6482 2016-05-12        kinaba: 				stk.top() += s[i];
27207f6482 2016-05-12        kinaba: 		}
27207f6482 2016-05-12        kinaba: 
27207f6482 2016-05-12        kinaba: 		return solve(req, stk.top());
27207f6482 2016-05-12        kinaba: 	}
27207f6482 2016-05-12        kinaba: 
27207f6482 2016-05-12        kinaba: 	int sig(const string& s)
27207f6482 2016-05-12        kinaba: 	{
27207f6482 2016-05-12        kinaba: 		int d = 0;
27207f6482 2016-05-12        kinaba: 		for(char c: s) d += (c=='(' ? +1 : -1);
27207f6482 2016-05-12        kinaba: 		return d;
27207f6482 2016-05-12        kinaba: 	}
27207f6482 2016-05-12        kinaba: 
27207f6482 2016-05-12        kinaba: 	// required to make |require| strings correct, |remaining| is the unconstrained supply of chars.
27207f6482 2016-05-12        kinaba: 	int solve(const vector<string>& require, const string& remaining)
27207f6482 2016-05-12        kinaba: 	{
27207f6482 2016-05-12        kinaba: 		//cerr << "[" << remaining << "]" << endl;
27207f6482 2016-05-12        kinaba: 
27207f6482 2016-05-12        kinaba: 		int cost = 0;
27207f6482 2016-05-12        kinaba: 
27207f6482 2016-05-12        kinaba: 		vector<int> sv;
27207f6482 2016-05-12        kinaba: 		for(auto r: require)
27207f6482 2016-05-12        kinaba: 		{
27207f6482 2016-05-12        kinaba: 			int sr = sig(r)/2;
27207f6482 2016-05-12        kinaba: 			sv.push_back(sr);
27207f6482 2016-05-12        kinaba: 
27207f6482 2016-05-12        kinaba: 			//cerr << ":: ["  << r << "] @ " << sr << endl;
27207f6482 2016-05-12        kinaba: 
27207f6482 2016-05-12        kinaba: 			int d = 0;
27207f6482 2016-05-12        kinaba: 			for(char& c: r)
27207f6482 2016-05-12        kinaba: 			{
27207f6482 2016-05-12        kinaba: 				if(sr>0 && d>0 && c=='(') c=')', --sr;
27207f6482 2016-05-12        kinaba: 				else if(sr<0 && c==')') c='(', ++sr;
27207f6482 2016-05-12        kinaba: 				d += (c=='(' ? +1 : -1);
27207f6482 2016-05-12        kinaba: 			}
27207f6482 2016-05-12        kinaba: 			cost += cost_single(r);
27207f6482 2016-05-12        kinaba: 		}
27207f6482 2016-05-12        kinaba: 
27207f6482 2016-05-12        kinaba: 		int ss = accumulate(sv.begin(), sv.end(), 0);
27207f6482 2016-05-12        kinaba: 		if(ss>0 && ss>count(remaining.begin(), remaining.end(), ')')) return -1;
27207f6482 2016-05-12        kinaba: 		if(ss<0 && -ss>count(remaining.begin(), remaining.end(), '(')) return -1;
27207f6482 2016-05-12        kinaba: 
27207f6482 2016-05-12        kinaba: 		int nega=0, posi=0; for(int z: sv) if(z<0) nega+=-z; else posi+=z;
27207f6482 2016-05-12        kinaba: 		return cost + max(nega, posi);
27207f6482 2016-05-12        kinaba: 	}
27207f6482 2016-05-12        kinaba: 
27207f6482 2016-05-12        kinaba: 	int cost_single(string r)
27207f6482 2016-05-12        kinaba: 	{
27207f6482 2016-05-12        kinaba: 		int cost = 0;
27207f6482 2016-05-12        kinaba: 		int d = 0;
27207f6482 2016-05-12        kinaba: 		for(char c: r) {
27207f6482 2016-05-12        kinaba: 			d += (c=='(' ? +1 : -1);
27207f6482 2016-05-12        kinaba: 			if(d==-1) ++cost, d=+1;
27207f6482 2016-05-12        kinaba: 		}
27207f6482 2016-05-12        kinaba: 		return cost;
27207f6482 2016-05-12        kinaba: 	}
27207f6482 2016-05-12        kinaba: };
27207f6482 2016-05-12        kinaba: 
27207f6482 2016-05-12        kinaba: // BEGIN CUT HERE
27207f6482 2016-05-12        kinaba: #include <ctime>
27207f6482 2016-05-12        kinaba: double start_time; string timer()
27207f6482 2016-05-12        kinaba:  { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
27207f6482 2016-05-12        kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
27207f6482 2016-05-12        kinaba:  { os << "{ ";
27207f6482 2016-05-12        kinaba:    for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
27207f6482 2016-05-12        kinaba:    os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
27207f6482 2016-05-12        kinaba: void verify_case(const int& Expected, const int& Received) {
27207f6482 2016-05-12        kinaba:  bool ok = (Expected == Received);
27207f6482 2016-05-12        kinaba:  if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
27207f6482 2016-05-12        kinaba:  cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
27207f6482 2016-05-12        kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
27207f6482 2016-05-12        kinaba: #define END	 verify_case(_, ParenthesesDiv1Medium().minSwaps(s, L, R));}
27207f6482 2016-05-12        kinaba: int main(){
27207f6482 2016-05-12        kinaba: 
27207f6482 2016-05-12        kinaba: CASE(0)
27207f6482 2016-05-12        kinaba: 	string s = ")(";
27207f6482 2016-05-12        kinaba: 	int L_[] = {0,0,0,0};
27207f6482 2016-05-12        kinaba: 	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_));
27207f6482 2016-05-12        kinaba: 	int R_[] = {1,1,1,1};
27207f6482 2016-05-12        kinaba: 	  vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_));
27207f6482 2016-05-12        kinaba: 	int _ = 1;
27207f6482 2016-05-12        kinaba: END
27207f6482 2016-05-12        kinaba: CASE(1)
27207f6482 2016-05-12        kinaba: 	string s = "(())";
27207f6482 2016-05-12        kinaba: 	int L_[] = {0,2};
27207f6482 2016-05-12        kinaba: 	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_));
27207f6482 2016-05-12        kinaba: 	int R_[] = {1,3};
27207f6482 2016-05-12        kinaba: 	  vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_));
27207f6482 2016-05-12        kinaba: 	int _ = 1;
27207f6482 2016-05-12        kinaba: END
27207f6482 2016-05-12        kinaba: CASE(2)
27207f6482 2016-05-12        kinaba: 	string s = "(((())";
27207f6482 2016-05-12        kinaba: 	int L_[] = {0,2};
27207f6482 2016-05-12        kinaba: 	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_));
27207f6482 2016-05-12        kinaba: 	int R_[] = {1,3};
27207f6482 2016-05-12        kinaba: 	  vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_));
27207f6482 2016-05-12        kinaba: 	int _ = 2;
27207f6482 2016-05-12        kinaba: END
27207f6482 2016-05-12        kinaba: CASE(3)
27207f6482 2016-05-12        kinaba: 	string s = "(((((((((";
27207f6482 2016-05-12        kinaba: 	int L_[] = {0,2};
27207f6482 2016-05-12        kinaba: 	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_));
27207f6482 2016-05-12        kinaba: 	int R_[] = {1,3};
27207f6482 2016-05-12        kinaba: 	  vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_));
27207f6482 2016-05-12        kinaba: 	int _ = -1;
27207f6482 2016-05-12        kinaba: END
27207f6482 2016-05-12        kinaba: CASE(4)
27207f6482 2016-05-12        kinaba: 	string s = "()()()()";
27207f6482 2016-05-12        kinaba: 	int L_[] = {0,0,0,0,2,2,2,4,4,6};
27207f6482 2016-05-12        kinaba: 	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_));
27207f6482 2016-05-12        kinaba: 	int R_[] = {1,3,5,7,3,5,7,5,7,7};
27207f6482 2016-05-12        kinaba: 	  vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_));
27207f6482 2016-05-12        kinaba: 	int _ = 0;
27207f6482 2016-05-12        kinaba: END
27207f6482 2016-05-12        kinaba: CASE(5)
27207f6482 2016-05-12        kinaba: 	string s = ")()(()()((())()()()()()()))(()())()()()(";
27207f6482 2016-05-12        kinaba: 	int L_[] = {3,5,17,25,35};
27207f6482 2016-05-12        kinaba: 	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_));
27207f6482 2016-05-12        kinaba: 	int R_[] = {12,10,30,30,38};
27207f6482 2016-05-12        kinaba: 	  vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_));
27207f6482 2016-05-12        kinaba: 	int _ = 3;
27207f6482 2016-05-12        kinaba: END
27207f6482 2016-05-12        kinaba: /*
27207f6482 2016-05-12        kinaba: CASE(6)
27207f6482 2016-05-12        kinaba: 	string s = ;
27207f6482 2016-05-12        kinaba: 	int L_[] = ;
27207f6482 2016-05-12        kinaba: 	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_));
27207f6482 2016-05-12        kinaba: 	int R_[] = ;
27207f6482 2016-05-12        kinaba: 	  vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_));
27207f6482 2016-05-12        kinaba: 	int _ = ;
27207f6482 2016-05-12        kinaba: END
27207f6482 2016-05-12        kinaba: CASE(7)
27207f6482 2016-05-12        kinaba: 	string s = ;
27207f6482 2016-05-12        kinaba: 	int L_[] = ;
27207f6482 2016-05-12        kinaba: 	  vector <int> L(L_, L_+sizeof(L_)/sizeof(*L_));
27207f6482 2016-05-12        kinaba: 	int R_[] = ;
27207f6482 2016-05-12        kinaba: 	  vector <int> R(R_, R_+sizeof(R_)/sizeof(*R_));
27207f6482 2016-05-12        kinaba: 	int _ = ;
27207f6482 2016-05-12        kinaba: END
27207f6482 2016-05-12        kinaba: */
27207f6482 2016-05-12        kinaba: }
27207f6482 2016-05-12        kinaba: // END CUT HERE