9f60383a2c 2012-06-16 kinaba: #include <iostream> 9f60383a2c 2012-06-16 kinaba: #include <sstream> 9f60383a2c 2012-06-16 kinaba: #include <iomanip> 9f60383a2c 2012-06-16 kinaba: #include <vector> 9f60383a2c 2012-06-16 kinaba: #include <string> 9f60383a2c 2012-06-16 kinaba: #include <map> 9f60383a2c 2012-06-16 kinaba: #include <set> 9f60383a2c 2012-06-16 kinaba: #include <algorithm> 9f60383a2c 2012-06-16 kinaba: #include <numeric> 9f60383a2c 2012-06-16 kinaba: #include <iterator> 9f60383a2c 2012-06-16 kinaba: #include <functional> 9f60383a2c 2012-06-16 kinaba: #include <complex> 9f60383a2c 2012-06-16 kinaba: #include <queue> 9f60383a2c 2012-06-16 kinaba: #include <stack> 9f60383a2c 2012-06-16 kinaba: #include <cmath> 9f60383a2c 2012-06-16 kinaba: #include <cassert> 9f60383a2c 2012-06-16 kinaba: using namespace std; 9f60383a2c 2012-06-16 kinaba: typedef long long LL; 9f60383a2c 2012-06-16 kinaba: typedef long double LD; 9f60383a2c 2012-06-16 kinaba: typedef complex<LD> CMP; 9f60383a2c 2012-06-16 kinaba: 9f60383a2c 2012-06-16 kinaba: class StrIIRec { public: 9f60383a2c 2012-06-16 kinaba: string recovstr(int n, int minInv, string minStr) 9f60383a2c 2012-06-16 kinaba: { 9f60383a2c 2012-06-16 kinaba: for(int i=0; i<n; ++i) 9f60383a2c 2012-06-16 kinaba: if(minStr.find(char('a'+i)) == string::npos) 9f60383a2c 2012-06-16 kinaba: minStr += char('a'+i); 9f60383a2c 2012-06-16 kinaba: 9f60383a2c 2012-06-16 kinaba: for(int i=n-1; i>=0; --i) { 9f60383a2c 2012-06-16 kinaba: string pre = minStr.substr(0,i); 9f60383a2c 2012-06-16 kinaba: string post = minStr.substr(i); 9f60383a2c 2012-06-16 kinaba: sort(post.rbegin(), post.rend()); 9f60383a2c 2012-06-16 kinaba: if( inv(pre+post) >= minInv ) 9f60383a2c 2012-06-16 kinaba: if(i==n-1) { 9f60383a2c 2012-06-16 kinaba: return pre+post; 9f60383a2c 2012-06-16 kinaba: } else { 9f60383a2c 2012-06-16 kinaba: string mid = ""; 9f60383a2c 2012-06-16 kinaba: for(int m=i; m<n; ++m) 9f60383a2c 2012-06-16 kinaba: { 9f60383a2c 2012-06-16 kinaba: for(int k=post.size()-1; k>=0; --k) 9f60383a2c 2012-06-16 kinaba: if((m==i && post[k]>minStr[i]) || (m!=i)) { 9f60383a2c 2012-06-16 kinaba: string mmid = mid + post[k]; 9f60383a2c 2012-06-16 kinaba: string ppost = post.substr(0,k) + post.substr(k+1); 9f60383a2c 2012-06-16 kinaba: if( inv(pre+mmid+ppost) >= minInv ) { 9f60383a2c 2012-06-16 kinaba: mid = mmid; 9f60383a2c 2012-06-16 kinaba: post = ppost; 9f60383a2c 2012-06-16 kinaba: goto nextM; 9f60383a2c 2012-06-16 kinaba: } 9f60383a2c 2012-06-16 kinaba: } 9f60383a2c 2012-06-16 kinaba: nextM:; 9f60383a2c 2012-06-16 kinaba: } 9f60383a2c 2012-06-16 kinaba: return pre + mid; 9f60383a2c 2012-06-16 kinaba: } 9f60383a2c 2012-06-16 kinaba: } 9f60383a2c 2012-06-16 kinaba: return "???"; 9f60383a2c 2012-06-16 kinaba: } 9f60383a2c 2012-06-16 kinaba: 9f60383a2c 2012-06-16 kinaba: int inv(const string& s) 9f60383a2c 2012-06-16 kinaba: { 9f60383a2c 2012-06-16 kinaba: int cnt = 0; 9f60383a2c 2012-06-16 kinaba: for(int i=0; i<s.size(); ++i) 9f60383a2c 2012-06-16 kinaba: for(int j=i+1; j<s.size(); ++j) 9f60383a2c 2012-06-16 kinaba: if(s[i]>s[j]) 9f60383a2c 2012-06-16 kinaba: ++cnt; 9f60383a2c 2012-06-16 kinaba: return cnt; 9f60383a2c 2012-06-16 kinaba: } 9f60383a2c 2012-06-16 kinaba: }; 9f60383a2c 2012-06-16 kinaba: 9f60383a2c 2012-06-16 kinaba: // BEGIN CUT HERE 9f60383a2c 2012-06-16 kinaba: #include <ctime> 9f60383a2c 2012-06-16 kinaba: double start_time; string timer() 9f60383a2c 2012-06-16 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 9f60383a2c 2012-06-16 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 9f60383a2c 2012-06-16 kinaba: { os << "{ "; 9f60383a2c 2012-06-16 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 9f60383a2c 2012-06-16 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 9f60383a2c 2012-06-16 kinaba: void verify_case(const string& Expected, const string& Received) { 9f60383a2c 2012-06-16 kinaba: bool ok = (Expected == Received); 9f60383a2c 2012-06-16 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 9f60383a2c 2012-06-16 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 9f60383a2c 2012-06-16 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 9f60383a2c 2012-06-16 kinaba: #define END verify_case(_, StrIIRec().recovstr(n, minInv, minStr));} 9f60383a2c 2012-06-16 kinaba: int main(){ 9f60383a2c 2012-06-16 kinaba: 9f60383a2c 2012-06-16 kinaba: CASE(0) 9f60383a2c 2012-06-16 kinaba: int n = 2; 9f60383a2c 2012-06-16 kinaba: int minInv = 1; 9f60383a2c 2012-06-16 kinaba: string minStr = "ab"; 9f60383a2c 2012-06-16 kinaba: string _ = "ba"; 9f60383a2c 2012-06-16 kinaba: END 9f60383a2c 2012-06-16 kinaba: CASE(1) 9f60383a2c 2012-06-16 kinaba: int n = 9; 9f60383a2c 2012-06-16 kinaba: int minInv = 1; 9f60383a2c 2012-06-16 kinaba: string minStr = "efcdgab"; 9f60383a2c 2012-06-16 kinaba: string _ = "efcdgabhi"; 9f60383a2c 2012-06-16 kinaba: END 9f60383a2c 2012-06-16 kinaba: CASE(2) 9f60383a2c 2012-06-16 kinaba: int n = 11; 9f60383a2c 2012-06-16 kinaba: int minInv = 55; 9f60383a2c 2012-06-16 kinaba: string minStr = "debgikjfc"; 9f60383a2c 2012-06-16 kinaba: string _ = "kjihgfedcba"; 9f60383a2c 2012-06-16 kinaba: END 9f60383a2c 2012-06-16 kinaba: CASE(3) 9f60383a2c 2012-06-16 kinaba: int n = 15; 9f60383a2c 2012-06-16 kinaba: int minInv = 0; 9f60383a2c 2012-06-16 kinaba: string minStr = "e"; 9f60383a2c 2012-06-16 kinaba: string _ = "eabcdfghijklmno"; 9f60383a2c 2012-06-16 kinaba: END 9f60383a2c 2012-06-16 kinaba: CASE(4) 9f60383a2c 2012-06-16 kinaba: int n = 9; 9f60383a2c 2012-06-16 kinaba: int minInv = 20; 9f60383a2c 2012-06-16 kinaba: string minStr = "fcdebiha"; 9f60383a2c 2012-06-16 kinaba: string _ = "fcdehigba"; 9f60383a2c 2012-06-16 kinaba: END 9f60383a2c 2012-06-16 kinaba: CASE(5) 9f60383a2c 2012-06-16 kinaba: int n = 20; 9f60383a2c 2012-06-16 kinaba: int minInv = 100; 9f60383a2c 2012-06-16 kinaba: string minStr = "abcdefghijklmnopqrst"; 9f60383a2c 2012-06-16 kinaba: string _ = "?"; 9f60383a2c 2012-06-16 kinaba: END 9f60383a2c 2012-06-16 kinaba: /* 9f60383a2c 2012-06-16 kinaba: CASE(6) 9f60383a2c 2012-06-16 kinaba: int n = ; 9f60383a2c 2012-06-16 kinaba: int minInv = ; 9f60383a2c 2012-06-16 kinaba: string minStr = ; 9f60383a2c 2012-06-16 kinaba: string _ = ; 9f60383a2c 2012-06-16 kinaba: END 9f60383a2c 2012-06-16 kinaba: */ 9f60383a2c 2012-06-16 kinaba: } 9f60383a2c 2012-06-16 kinaba: // END CUT HERE