Artifact Content
Not logged in

Artifact 473b414db01c6264fa7b61af962981d841d0b126


#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 TheBrickTowerMediumDivOne { public:
	vector <int> find(vector<int> heights)
	{
		const int N = heights.size();
		const int best = best_for(heights);
		vector<bool> used(N, false);
		vector<int> answer;
		vector<int> current;
		int current_score = 0;
		for(int i=0; i<N; ++i)
		{
			for(int k=0; k<N; ++k) if(!used[k])
			{
				vector<int> hh;
				for(int a=0; a<N; ++a) if(!used[a] && a!=k)
					hh.push_back(heights[a]);
				int ss = current_score;
				if(!current.empty()) ss += max(current.back(), heights[k]);
				ss += best_for(heights[k], hh);
				if( ss == best ) {
					answer.push_back(k);
					if(!current.empty()) current_score += max(current.back(), heights[k]);
					current.push_back(heights[k]);
					used[k] = true;
					break;
				}
			}
		}
		return answer;
	}

	int best_for(vector<int> h)
	{
		sort(h.begin(), h.end());
		int best = 0;
		for(int i=0; i+1<h.size(); ++i)
			best += max(h[i], h[i+1]);
		return best;
	}

	int best_for(int h0, vector<int> h)
	{
		if(h.empty())
			return 0;

		sort(h.begin(), h.end());
		int best = max(h0, h[0]);
		for(int i=0; i+1<h.size(); ++i)
			best += max(h[i], h[i+1]);
		return best;
	}
};

// 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 vector <int>& Expected, const vector <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(_, TheBrickTowerMediumDivOne().find(heights));}
int main(){

CASE(0)
	int heights_[] = {4, 7, 5};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int __[] = {0, 2, 1 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(1)
	int heights_[] = {4, 4, 4, 4, 4, 4, 4};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int __[] = {0, 1, 2, 3, 4, 5, 6 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(2)
	int heights_[] = {2, 3, 3, 2};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int __[] = {0, 3, 1, 2 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(3)
	int heights_[] = {13, 32, 38, 25, 43, 47, 6};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	int __[] = {0, 6, 3, 1, 2, 4, 5 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(4)
	int heights_[] = {28,36,27,33,30,18,16,23,39,31,4,40,41,41,30,43,45,16,5,26,35,19,2,40,27,3,37,46,5,34,22,25,19,6,45,6,45,36,40,13,29,25,10,23,6,3,21};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	  vector <int> _;
END
CASE(5)
	int heights_[] = {7,34,5,15,43,8,36,5,42,22,37,45,46,38,40,2,3,22,35,23,14,31,26,1,43,11,24,41,21,8,11,28,46,21,42,5,9,18,4,3,47,15,32,16,26,45,34};
	  vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); 
	  vector <int> _;
END
}
// END CUT HERE