Artifact Content
Not logged in

Artifact 124c7549ad607d0d34e0bb72f568905b8c87497a


#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 WhiteSpaceEditing { public:
	int getMinimum(vector <int> lines)
	{
		set<int> slen(lines.begin(), lines.end());
		slen.insert(0);

		vector<int> len(slen.begin(), slen.end());
		memo.assign(lines.size()*(lines.size()+1)*len.size(), -1);
		return rec(lines, 0, lines.size(), len, 0) + (lines.size()-1);
	}

	vector<int> memo;
	int rec(const vector<int>& lines, int s, int e,
	        const vector<int>& len, int cur_len) {
		if(s+1 == e)
			return abs(lines[s] - len[cur_len]);

		int key = (s*(lines.size()+1)+e)*len.size()+cur_len;
		if(memo[key] != -1)
			return memo[key];

		int result = 0x1fffffff;
		for(int base_len=0; base_len<len.size(); ++base_len)
			for(int m=s+1; m<e; ++m)
				result = min(result,
					rec(lines, s, m, len, base_len) +
					rec(lines, m, e, len, base_len) + abs(len[cur_len] - len[base_len]));
		return memo[key] = result;
	}
};

// 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 int& Expected, const 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(_, WhiteSpaceEditing().getMinimum(lines));}
int main(){

CASE(0)
	int lines_[] = { 3, 2, 3 };
	  vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 
	int _ = 6; 
END
CASE(1)
	int lines_[] = { 0 };
	  vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 
	int _ = 0; 
END
CASE(2)
	int lines_[] = { 1, 2, 4 }
;
	  vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 
	int _ = 6; 
END
CASE(3)
	int lines_[] = { 250, 105, 155, 205, 350 }
;
	  vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 
	int _ = 499; 
END
/*
CASE(4)
	int lines_[] = ;
	  vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 
	int _ = ; 
END
CASE(5)
	int lines_[] = ;
	  vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); 
	int _ = ; 
END
*/
}
// END CUT HERE