6b700c1e67 2013-06-04 kinaba: #include <iostream> 6b700c1e67 2013-06-04 kinaba: #include <sstream> 6b700c1e67 2013-06-04 kinaba: #include <iomanip> 6b700c1e67 2013-06-04 kinaba: #include <vector> 6b700c1e67 2013-06-04 kinaba: #include <string> 6b700c1e67 2013-06-04 kinaba: #include <map> 6b700c1e67 2013-06-04 kinaba: #include <set> 6b700c1e67 2013-06-04 kinaba: #include <algorithm> 6b700c1e67 2013-06-04 kinaba: #include <numeric> 6b700c1e67 2013-06-04 kinaba: #include <iterator> 6b700c1e67 2013-06-04 kinaba: #include <functional> 6b700c1e67 2013-06-04 kinaba: #include <complex> 6b700c1e67 2013-06-04 kinaba: #include <queue> 6b700c1e67 2013-06-04 kinaba: #include <stack> 6b700c1e67 2013-06-04 kinaba: #include <cmath> 6b700c1e67 2013-06-04 kinaba: #include <cassert> 6b700c1e67 2013-06-04 kinaba: using namespace std; 6b700c1e67 2013-06-04 kinaba: typedef long long LL; 6b700c1e67 2013-06-04 kinaba: typedef long double LD; 6b700c1e67 2013-06-04 kinaba: typedef complex<double> CMP; 6b700c1e67 2013-06-04 kinaba: 6b700c1e67 2013-06-04 kinaba: class ShoutterDiv1 { public: 6b700c1e67 2013-06-04 kinaba: int count(vector <string> s1000, vector <string> s100, vector <string> s10, vector <string> s1, vector <string> t1000, vector <string> t100, vector <string> t10, vector <string> t1) 6b700c1e67 2013-06-04 kinaba: { 6b700c1e67 2013-06-04 kinaba: vector<int> s; 6b700c1e67 2013-06-04 kinaba: { 6b700c1e67 2013-06-04 kinaba: string S1000 = accumulate(s1000.begin(), s1000.end(), string()); 6b700c1e67 2013-06-04 kinaba: string S100 = accumulate(s100.begin() , s100.end() , string()); 6b700c1e67 2013-06-04 kinaba: string S10 = accumulate(s10.begin() , s10.end() , string()); 6b700c1e67 2013-06-04 kinaba: string S1 = accumulate(s1.begin() , s1.end() , string()); 6b700c1e67 2013-06-04 kinaba: for(int i=0; i<S1.size(); ++i) 6b700c1e67 2013-06-04 kinaba: s.push_back(atoi((string(1, S1000[i])+S100[i]+S10[i]+S1[i]).c_str())); 6b700c1e67 2013-06-04 kinaba: } 6b700c1e67 2013-06-04 kinaba: vector<int> t; 6b700c1e67 2013-06-04 kinaba: { 6b700c1e67 2013-06-04 kinaba: string T1000 = accumulate(t1000.begin(), t1000.end(), string()); 6b700c1e67 2013-06-04 kinaba: string T100 = accumulate(t100.begin(), t100.end(), string()); 6b700c1e67 2013-06-04 kinaba: string T10 = accumulate(t10.begin(), t10.end(), string()); 6b700c1e67 2013-06-04 kinaba: string T1 = accumulate(t1.begin(), t1.end(), string()); 6b700c1e67 2013-06-04 kinaba: for(int i=0; i<T1.size(); ++i) 6b700c1e67 2013-06-04 kinaba: t.push_back(atoi((string(1, T1000[i])+T100[i]+T10[i]+T1[i]).c_str())); 6b700c1e67 2013-06-04 kinaba: } 6b700c1e67 2013-06-04 kinaba: vector<pair<int,int> > st; 6b700c1e67 2013-06-04 kinaba: for(int i=0; i<s.size(); ++i) 6b700c1e67 2013-06-04 kinaba: st.push_back(make_pair(s[i], t[i])); 6b700c1e67 2013-06-04 kinaba: sort(st.begin(), st.end()); 6b700c1e67 2013-06-04 kinaba: return solve(st); 6b700c1e67 2013-06-04 kinaba: } 6b700c1e67 2013-06-04 kinaba: 6b700c1e67 2013-06-04 kinaba: int solve(const vector<pair<int,int> >& st) 6b700c1e67 2013-06-04 kinaba: { 6b700c1e67 2013-06-04 kinaba: const int N = st.size(); 6b700c1e67 2013-06-04 kinaba: 6b700c1e67 2013-06-04 kinaba: int L = 99999; // the time first rabbit leaves. 6b700c1e67 2013-06-04 kinaba: int R = 0; // the time last rabbit comes. 6b700c1e67 2013-06-04 kinaba: for(int i=0; i<N; ++i) { 6b700c1e67 2013-06-04 kinaba: int l = st[i].first; 6b700c1e67 2013-06-04 kinaba: int r = st[i].second; 6b700c1e67 2013-06-04 kinaba: L = min(L, r); 6b700c1e67 2013-06-04 kinaba: R = max(R, l); 6b700c1e67 2013-06-04 kinaba: } 6b700c1e67 2013-06-04 kinaba: 6b700c1e67 2013-06-04 kinaba: // fill [L,R] with minimum number of intervals. 6b700c1e67 2013-06-04 kinaba: // must include st[i] for each i. 6b700c1e67 2013-06-04 kinaba: 6b700c1e67 2013-06-04 kinaba: int total = 0; 6b700c1e67 2013-06-04 kinaba: for(int i=0; i<N; ++i) { 6b700c1e67 2013-06-04 kinaba: int c = cost(st, N, i, L, R); 6b700c1e67 2013-06-04 kinaba: if(c==-1) 6b700c1e67 2013-06-04 kinaba: return -1; 6b700c1e67 2013-06-04 kinaba: total += c - 1; 6b700c1e67 2013-06-04 kinaba: } 6b700c1e67 2013-06-04 kinaba: return total; 6b700c1e67 2013-06-04 kinaba: } 6b700c1e67 2013-06-04 kinaba: 6b700c1e67 2013-06-04 kinaba: int cost(const vector<pair<int,int> >& st, int N, int I, int L, int R) 6b700c1e67 2013-06-04 kinaba: { 6b700c1e67 2013-06-04 kinaba: bool I_used = false; 6b700c1e67 2013-06-04 kinaba: int covered = L; 6b700c1e67 2013-06-04 kinaba: int used = 0; 6b700c1e67 2013-06-04 kinaba: for(int ks=0; ks<N; ) 6b700c1e67 2013-06-04 kinaba: { 6b700c1e67 2013-06-04 kinaba: int ke=ks; 6b700c1e67 2013-06-04 kinaba: while(ke<N && st[ke].first<=covered) 6b700c1e67 2013-06-04 kinaba: ++ke; 6b700c1e67 2013-06-04 kinaba: if(ke==ks) 6b700c1e67 2013-06-04 kinaba: break; 6b700c1e67 2013-06-04 kinaba: 6b700c1e67 2013-06-04 kinaba: if(ks<=I && I<ke && !I_used) 6b700c1e67 2013-06-04 kinaba: { 6b700c1e67 2013-06-04 kinaba: ++used; 6b700c1e67 2013-06-04 kinaba: I_used = true; 6b700c1e67 2013-06-04 kinaba: covered = max(covered, st[I].second); 6b700c1e67 2013-06-04 kinaba: } 6b700c1e67 2013-06-04 kinaba: else 6b700c1e67 2013-06-04 kinaba: { 6b700c1e67 2013-06-04 kinaba: if(covered < R) { 6b700c1e67 2013-06-04 kinaba: int bestRight = 0; 6b700c1e67 2013-06-04 kinaba: for(int i=ks; i<ke; ++i) if(i!=I) 6b700c1e67 2013-06-04 kinaba: bestRight = max(bestRight, st[i].second); 6b700c1e67 2013-06-04 kinaba: if(covered < bestRight) { 6b700c1e67 2013-06-04 kinaba: ++used; 6b700c1e67 2013-06-04 kinaba: covered = max(covered, bestRight); 6b700c1e67 2013-06-04 kinaba: } 6b700c1e67 2013-06-04 kinaba: } 6b700c1e67 2013-06-04 kinaba: ks = ke; 6b700c1e67 2013-06-04 kinaba: } 6b700c1e67 2013-06-04 kinaba: } 6b700c1e67 2013-06-04 kinaba: return (R<=covered ? used : -1); 6b700c1e67 2013-06-04 kinaba: } 6b700c1e67 2013-06-04 kinaba: }; 6b700c1e67 2013-06-04 kinaba: 6b700c1e67 2013-06-04 kinaba: // BEGIN CUT HERE 6b700c1e67 2013-06-04 kinaba: #include <ctime> 6b700c1e67 2013-06-04 kinaba: double start_time; string timer() 6b700c1e67 2013-06-04 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 6b700c1e67 2013-06-04 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 6b700c1e67 2013-06-04 kinaba: { os << "{ "; 6b700c1e67 2013-06-04 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 6b700c1e67 2013-06-04 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 6b700c1e67 2013-06-04 kinaba: void verify_case(const int& Expected, const int& Received) { 6b700c1e67 2013-06-04 kinaba: bool ok = (Expected == Received); 6b700c1e67 2013-06-04 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 6b700c1e67 2013-06-04 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 6b700c1e67 2013-06-04 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 6b700c1e67 2013-06-04 kinaba: #define END verify_case(_, ShoutterDiv1().count(s1000, s100, s10, s1, t1000, t100, t10, t1));} 6b700c1e67 2013-06-04 kinaba: int main(){ 6b700c1e67 2013-06-04 kinaba: 6b700c1e67 2013-06-04 kinaba: CASE(0) 6b700c1e67 2013-06-04 kinaba: string s1000_[] = {"22", "2"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s1000(s1000_, s1000_+sizeof(s1000_)/sizeof(*s1000_)); 6b700c1e67 2013-06-04 kinaba: string s100_[] = {"00", "0"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s100(s100_, s100_+sizeof(s100_)/sizeof(*s100_)); 6b700c1e67 2013-06-04 kinaba: string s10_[] = {"11", "1"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s10(s10_, s10_+sizeof(s10_)/sizeof(*s10_)); 6b700c1e67 2013-06-04 kinaba: string s1_[] = {"21", "4"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s1(s1_, s1_+sizeof(s1_)/sizeof(*s1_)); 6b700c1e67 2013-06-04 kinaba: string t1000_[] = {"22", "2"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t1000(t1000_, t1000_+sizeof(t1000_)/sizeof(*t1000_)); 6b700c1e67 2013-06-04 kinaba: string t100_[] = {"00", "0"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t100(t100_, t100_+sizeof(t100_)/sizeof(*t100_)); 6b700c1e67 2013-06-04 kinaba: string t10_[] = {"11", "1"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t10(t10_, t10_+sizeof(t10_)/sizeof(*t10_)); 6b700c1e67 2013-06-04 kinaba: string t1_[] = {"43", "6"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t1(t1_, t1_+sizeof(t1_)/sizeof(*t1_)); 6b700c1e67 2013-06-04 kinaba: int _ = 2; 6b700c1e67 2013-06-04 kinaba: END 6b700c1e67 2013-06-04 kinaba: CASE(1) 6b700c1e67 2013-06-04 kinaba: string s1000_[] = {"00"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s1000(s1000_, s1000_+sizeof(s1000_)/sizeof(*s1000_)); 6b700c1e67 2013-06-04 kinaba: string s100_[] = {"00"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s100(s100_, s100_+sizeof(s100_)/sizeof(*s100_)); 6b700c1e67 2013-06-04 kinaba: string s10_[] = {"00"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s10(s10_, s10_+sizeof(s10_)/sizeof(*s10_)); 6b700c1e67 2013-06-04 kinaba: string s1_[] = {"13"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s1(s1_, s1_+sizeof(s1_)/sizeof(*s1_)); 6b700c1e67 2013-06-04 kinaba: string t1000_[] = {"00"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t1000(t1000_, t1000_+sizeof(t1000_)/sizeof(*t1000_)); 6b700c1e67 2013-06-04 kinaba: string t100_[] = {"00"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t100(t100_, t100_+sizeof(t100_)/sizeof(*t100_)); 6b700c1e67 2013-06-04 kinaba: string t10_[] = {"00"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t10(t10_, t10_+sizeof(t10_)/sizeof(*t10_)); 6b700c1e67 2013-06-04 kinaba: string t1_[] = {"24"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t1(t1_, t1_+sizeof(t1_)/sizeof(*t1_)); 6b700c1e67 2013-06-04 kinaba: int _ = -1; 6b700c1e67 2013-06-04 kinaba: END 6b700c1e67 2013-06-04 kinaba: CASE(2) 6b700c1e67 2013-06-04 kinaba: string s1000_[] = {"0000"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s1000(s1000_, s1000_+sizeof(s1000_)/sizeof(*s1000_)); 6b700c1e67 2013-06-04 kinaba: string s100_[] = {"0000"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s100(s100_, s100_+sizeof(s100_)/sizeof(*s100_)); 6b700c1e67 2013-06-04 kinaba: string s10_[] = {"0000"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s10(s10_, s10_+sizeof(s10_)/sizeof(*s10_)); 6b700c1e67 2013-06-04 kinaba: string s1_[] = {"1234"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s1(s1_, s1_+sizeof(s1_)/sizeof(*s1_)); 6b700c1e67 2013-06-04 kinaba: string t1000_[] = {"0000"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t1000(t1000_, t1000_+sizeof(t1000_)/sizeof(*t1000_)); 6b700c1e67 2013-06-04 kinaba: string t100_[] = {"0000"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t100(t100_, t100_+sizeof(t100_)/sizeof(*t100_)); 6b700c1e67 2013-06-04 kinaba: string t10_[] = {"0000"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t10(t10_, t10_+sizeof(t10_)/sizeof(*t10_)); 6b700c1e67 2013-06-04 kinaba: string t1_[] = {"2345"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t1(t1_, t1_+sizeof(t1_)/sizeof(*t1_)); 6b700c1e67 2013-06-04 kinaba: int _ = 6; 6b700c1e67 2013-06-04 kinaba: END 6b700c1e67 2013-06-04 kinaba: CASE(3) 6b700c1e67 2013-06-04 kinaba: string s1000_[] = {"0000000000"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s1000(s1000_, s1000_+sizeof(s1000_)/sizeof(*s1000_)); 6b700c1e67 2013-06-04 kinaba: string s100_[] = {"0000000000"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s100(s100_, s100_+sizeof(s100_)/sizeof(*s100_)); 6b700c1e67 2013-06-04 kinaba: string s10_[] = {"0000000000"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s10(s10_, s10_+sizeof(s10_)/sizeof(*s10_)); 6b700c1e67 2013-06-04 kinaba: string s1_[] = {"7626463146"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s1(s1_, s1_+sizeof(s1_)/sizeof(*s1_)); 6b700c1e67 2013-06-04 kinaba: string t1000_[] = {"0000000000"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t1000(t1000_, t1000_+sizeof(t1000_)/sizeof(*t1000_)); 6b700c1e67 2013-06-04 kinaba: string t100_[] = {"0000000000"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t100(t100_, t100_+sizeof(t100_)/sizeof(*t100_)); 6b700c1e67 2013-06-04 kinaba: string t10_[] = {"0000000000"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t10(t10_, t10_+sizeof(t10_)/sizeof(*t10_)); 6b700c1e67 2013-06-04 kinaba: string t1_[] = {"9927686479"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t1(t1_, t1_+sizeof(t1_)/sizeof(*t1_)); 6b700c1e67 2013-06-04 kinaba: int _ = 18; 6b700c1e67 2013-06-04 kinaba: END 6b700c1e67 2013-06-04 kinaba: CASE(4) 6b700c1e67 2013-06-04 kinaba: string s1000_[] = {"00000000000000000000000000000000000000000000000000"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s1000(s1000_, s1000_+sizeof(s1000_)/sizeof(*s1000_)); 6b700c1e67 2013-06-04 kinaba: string s100_[] = {"00000000000000000000000000000000000000000000000000"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s100(s100_, s100_+sizeof(s100_)/sizeof(*s100_)); 6b700c1e67 2013-06-04 kinaba: string s10_[] = {"50353624751857130208544645495168271486083954769538"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s10(s10_, s10_+sizeof(s10_)/sizeof(*s10_)); 6b700c1e67 2013-06-04 kinaba: string s1_[] = {"85748487990028258641117783760944852941545064635928"}; 6b700c1e67 2013-06-04 kinaba: vector <string> s1(s1_, s1_+sizeof(s1_)/sizeof(*s1_)); 6b700c1e67 2013-06-04 kinaba: string t1000_[] = {"00000000000000000000000000000000000000000000000000"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t1000(t1000_, t1000_+sizeof(t1000_)/sizeof(*t1000_)); 6b700c1e67 2013-06-04 kinaba: string t100_[] = {"00000000000000000000000000000000000000000000000000"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t100(t100_, t100_+sizeof(t100_)/sizeof(*t100_)); 6b700c1e67 2013-06-04 kinaba: string t10_[] = {"61465744851859252308555855596388482696094965779649"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t10(t10_, t10_+sizeof(t10_)/sizeof(*t10_)); 6b700c1e67 2013-06-04 kinaba: string t1_[] = {"37620749792666153778227385275518278477865684777411"}; 6b700c1e67 2013-06-04 kinaba: vector <string> t1(t1_, t1_+sizeof(t1_)/sizeof(*t1_)); 6b700c1e67 2013-06-04 kinaba: int _ = 333; 6b700c1e67 2013-06-04 kinaba: END 6b700c1e67 2013-06-04 kinaba: /* 6b700c1e67 2013-06-04 kinaba: CASE(5) 6b700c1e67 2013-06-04 kinaba: string s1000_[] = ; 6b700c1e67 2013-06-04 kinaba: vector <string> s1000(s1000_, s1000_+sizeof(s1000_)/sizeof(*s1000_)); 6b700c1e67 2013-06-04 kinaba: string s100_[] = ; 6b700c1e67 2013-06-04 kinaba: vector <string> s100(s100_, s100_+sizeof(s100_)/sizeof(*s100_)); 6b700c1e67 2013-06-04 kinaba: string s10_[] = ; 6b700c1e67 2013-06-04 kinaba: vector <string> s10(s10_, s10_+sizeof(s10_)/sizeof(*s10_)); 6b700c1e67 2013-06-04 kinaba: string s1_[] = ; 6b700c1e67 2013-06-04 kinaba: vector <string> s1(s1_, s1_+sizeof(s1_)/sizeof(*s1_)); 6b700c1e67 2013-06-04 kinaba: string t1000_[] = ; 6b700c1e67 2013-06-04 kinaba: vector <string> t1000(t1000_, t1000_+sizeof(t1000_)/sizeof(*t1000_)); 6b700c1e67 2013-06-04 kinaba: string t100_[] = ; 6b700c1e67 2013-06-04 kinaba: vector <string> t100(t100_, t100_+sizeof(t100_)/sizeof(*t100_)); 6b700c1e67 2013-06-04 kinaba: string t10_[] = ; 6b700c1e67 2013-06-04 kinaba: vector <string> t10(t10_, t10_+sizeof(t10_)/sizeof(*t10_)); 6b700c1e67 2013-06-04 kinaba: string t1_[] = ; 6b700c1e67 2013-06-04 kinaba: vector <string> t1(t1_, t1_+sizeof(t1_)/sizeof(*t1_)); 6b700c1e67 2013-06-04 kinaba: int _ = ; 6b700c1e67 2013-06-04 kinaba: END 6b700c1e67 2013-06-04 kinaba: CASE(6) 6b700c1e67 2013-06-04 kinaba: string s1000_[] = ; 6b700c1e67 2013-06-04 kinaba: vector <string> s1000(s1000_, s1000_+sizeof(s1000_)/sizeof(*s1000_)); 6b700c1e67 2013-06-04 kinaba: string s100_[] = ; 6b700c1e67 2013-06-04 kinaba: vector <string> s100(s100_, s100_+sizeof(s100_)/sizeof(*s100_)); 6b700c1e67 2013-06-04 kinaba: string s10_[] = ; 6b700c1e67 2013-06-04 kinaba: vector <string> s10(s10_, s10_+sizeof(s10_)/sizeof(*s10_)); 6b700c1e67 2013-06-04 kinaba: string s1_[] = ; 6b700c1e67 2013-06-04 kinaba: vector <string> s1(s1_, s1_+sizeof(s1_)/sizeof(*s1_)); 6b700c1e67 2013-06-04 kinaba: string t1000_[] = ; 6b700c1e67 2013-06-04 kinaba: vector <string> t1000(t1000_, t1000_+sizeof(t1000_)/sizeof(*t1000_)); 6b700c1e67 2013-06-04 kinaba: string t100_[] = ; 6b700c1e67 2013-06-04 kinaba: vector <string> t100(t100_, t100_+sizeof(t100_)/sizeof(*t100_)); 6b700c1e67 2013-06-04 kinaba: string t10_[] = ; 6b700c1e67 2013-06-04 kinaba: vector <string> t10(t10_, t10_+sizeof(t10_)/sizeof(*t10_)); 6b700c1e67 2013-06-04 kinaba: string t1_[] = ; 6b700c1e67 2013-06-04 kinaba: vector <string> t1(t1_, t1_+sizeof(t1_)/sizeof(*t1_)); 6b700c1e67 2013-06-04 kinaba: int _ = ; 6b700c1e67 2013-06-04 kinaba: END 6b700c1e67 2013-06-04 kinaba: */ 6b700c1e67 2013-06-04 kinaba: } 6b700c1e67 2013-06-04 kinaba: // END CUT HERE