Artifact Content
Not logged in

Artifact 44a4e17ed83b18736d1ba9159e3e040ba2a81109


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;

class StrIIRec { public:
	string recovstr(int n, int minInv, string minStr)
	{
		for(int i=0; i<n; ++i)
			if(minStr.find(char('a'+i)) == string::npos)
				minStr += char('a'+i);

		for(int i=n-1; i>=0; --i) {
			string pre = minStr.substr(0,i);
			string post = minStr.substr(i);
			sort(post.rbegin(), post.rend());
			if( inv(pre+post) >= minInv )
				if(i==n-1) {
					return pre+post;
				} else {
					string mid = "";
					for(int m=i; m<n; ++m)
					{
						for(int k=post.size()-1; k>=0; --k)
							if((m==i && post[k]>minStr[i]) || (m!=i)) {
								string mmid = mid + post[k];
								string ppost = post.substr(0,k) + post.substr(k+1);
								if( inv(pre+mmid+ppost) >= minInv )  {
									mid = mmid;
									post = ppost;
									goto nextM;
								}
							}
					nextM:;
					}
					return pre + mid;
				}
		}
		return "???";
	}

	int inv(const string& s)
	{
		int cnt = 0;
		for(int i=0; i<s.size(); ++i)
			for(int j=i+1; j<s.size(); ++j)
				if(s[i]>s[j])
					++cnt;
		return cnt;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const string& Expected, const string& 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(_, StrIIRec().recovstr(n, minInv, minStr));}
int main(){

CASE(0)
	int n = 2; 
	int minInv = 1; 
	string minStr = "ab"; 
	string _ = "ba"; 
END
CASE(1)
	int n = 9; 
	int minInv = 1; 
	string minStr = "efcdgab"; 
	string _ = "efcdgabhi"; 
END
CASE(2)
	int n = 11; 
	int minInv = 55; 
	string minStr = "debgikjfc"; 
	string _ = "kjihgfedcba"; 
END
CASE(3)
	int n = 15; 
	int minInv = 0; 
	string minStr = "e"; 
	string _ = "eabcdfghijklmno"; 
END
CASE(4)
	int n = 9; 
	int minInv = 20; 
	string minStr = "fcdebiha"; 
	string _ = "fcdehigba"; 
END
CASE(5)
	int n = 20; 
	int minInv = 100; 
	string minStr = "abcdefghijklmnopqrst"; 
	string _ = "?"; 
END
/*
CASE(6)
	int n = ; 
	int minInv = ; 
	string minStr = ; 
	string _ = ; 
END
*/
}
// END CUT HERE