b57cc94f1b 2011-10-08 kinaba: #include <iostream> b57cc94f1b 2011-10-08 kinaba: #include <sstream> b57cc94f1b 2011-10-08 kinaba: #include <iomanip> b57cc94f1b 2011-10-08 kinaba: #include <vector> b57cc94f1b 2011-10-08 kinaba: #include <string> b57cc94f1b 2011-10-08 kinaba: #include <map> b57cc94f1b 2011-10-08 kinaba: #include <set> b57cc94f1b 2011-10-08 kinaba: #include <algorithm> b57cc94f1b 2011-10-08 kinaba: #include <numeric> b57cc94f1b 2011-10-08 kinaba: #include <iterator> b57cc94f1b 2011-10-08 kinaba: #include <functional> b57cc94f1b 2011-10-08 kinaba: #include <complex> b57cc94f1b 2011-10-08 kinaba: #include <queue> b57cc94f1b 2011-10-08 kinaba: #include <stack> b57cc94f1b 2011-10-08 kinaba: #include <cmath> b57cc94f1b 2011-10-08 kinaba: #include <cassert> b57cc94f1b 2011-10-08 kinaba: #include <cstring> b57cc94f1b 2011-10-08 kinaba: using namespace std; b57cc94f1b 2011-10-08 kinaba: typedef long long LL; b57cc94f1b 2011-10-08 kinaba: typedef complex<double> CMP; b57cc94f1b 2011-10-08 kinaba: b57cc94f1b 2011-10-08 kinaba: class WhiteSpaceEditing { public: b57cc94f1b 2011-10-08 kinaba: int getMinimum(vector <int> lines) b57cc94f1b 2011-10-08 kinaba: { b57cc94f1b 2011-10-08 kinaba: const int N = lines.size(); b57cc94f1b 2011-10-08 kinaba: const int V = *max_element(lines.begin(), lines.end()); b57cc94f1b 2011-10-08 kinaba: vector<int> dp(V+1), dp_neo(V+1); b57cc94f1b 2011-10-08 kinaba: for(int v=0; v<=V; ++v) b57cc94f1b 2011-10-08 kinaba: dp[v] = abs(v - lines[N-1]); b57cc94f1b 2011-10-08 kinaba: for(int i=N-2; i>=0; --i) b57cc94f1b 2011-10-08 kinaba: { b57cc94f1b 2011-10-08 kinaba: int best = INT_MAX; b57cc94f1b 2011-10-08 kinaba: for(int v=lines[i]; v>=0; --v) { b57cc94f1b 2011-10-08 kinaba: best = min(best, dp[v]); b57cc94f1b 2011-10-08 kinaba: dp_neo[v] = abs(v - lines[i]) + best; b57cc94f1b 2011-10-08 kinaba: } b57cc94f1b 2011-10-08 kinaba: best = INT_MAX; b57cc94f1b 2011-10-08 kinaba: for(int v=lines[i]; v<=V; ++v) { b57cc94f1b 2011-10-08 kinaba: best = min(best, dp[v]); b57cc94f1b 2011-10-08 kinaba: dp_neo[v] = abs(v - lines[i]) + best; b57cc94f1b 2011-10-08 kinaba: } b57cc94f1b 2011-10-08 kinaba: dp.swap(dp_neo); b57cc94f1b 2011-10-08 kinaba: } b57cc94f1b 2011-10-08 kinaba: return dp[0] + N-1; b57cc94f1b 2011-10-08 kinaba: } b57cc94f1b 2011-10-08 kinaba: }; b57cc94f1b 2011-10-08 kinaba: b57cc94f1b 2011-10-08 kinaba: // BEGIN CUT HERE b57cc94f1b 2011-10-08 kinaba: #include <ctime> b57cc94f1b 2011-10-08 kinaba: double start_time; string timer() b57cc94f1b 2011-10-08 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } b57cc94f1b 2011-10-08 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) b57cc94f1b 2011-10-08 kinaba: { os << "{ "; b57cc94f1b 2011-10-08 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) b57cc94f1b 2011-10-08 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } b57cc94f1b 2011-10-08 kinaba: void verify_case(const int& Expected, const int& Received) { b57cc94f1b 2011-10-08 kinaba: bool ok = (Expected == Received); b57cc94f1b 2011-10-08 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; b57cc94f1b 2011-10-08 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } b57cc94f1b 2011-10-08 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); b57cc94f1b 2011-10-08 kinaba: #define END verify_case(_, WhiteSpaceEditing().getMinimum(lines));} b57cc94f1b 2011-10-08 kinaba: int main(){ b57cc94f1b 2011-10-08 kinaba: b57cc94f1b 2011-10-08 kinaba: CASE(0) b57cc94f1b 2011-10-08 kinaba: int lines_[] = { 3, 2, 3 }; b57cc94f1b 2011-10-08 kinaba: vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); b57cc94f1b 2011-10-08 kinaba: int _ = 6; b57cc94f1b 2011-10-08 kinaba: END b57cc94f1b 2011-10-08 kinaba: CASE(1) b57cc94f1b 2011-10-08 kinaba: int lines_[] = { 0 }; b57cc94f1b 2011-10-08 kinaba: vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); b57cc94f1b 2011-10-08 kinaba: int _ = 0; b57cc94f1b 2011-10-08 kinaba: END b57cc94f1b 2011-10-08 kinaba: CASE(2) b57cc94f1b 2011-10-08 kinaba: int lines_[] = { 1, 2, 4 } b57cc94f1b 2011-10-08 kinaba: ; b57cc94f1b 2011-10-08 kinaba: vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); b57cc94f1b 2011-10-08 kinaba: int _ = 6; b57cc94f1b 2011-10-08 kinaba: END b57cc94f1b 2011-10-08 kinaba: CASE(3) b57cc94f1b 2011-10-08 kinaba: int lines_[] = { 250, 105, 155, 205, 350 } b57cc94f1b 2011-10-08 kinaba: ; b57cc94f1b 2011-10-08 kinaba: vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); b57cc94f1b 2011-10-08 kinaba: int _ = 499; b57cc94f1b 2011-10-08 kinaba: END b57cc94f1b 2011-10-08 kinaba: CASE(4) b57cc94f1b 2011-10-08 kinaba: int lines_[] = {0}; b57cc94f1b 2011-10-08 kinaba: vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); b57cc94f1b 2011-10-08 kinaba: int _ = 0; b57cc94f1b 2011-10-08 kinaba: END b57cc94f1b 2011-10-08 kinaba: CASE(5) b57cc94f1b 2011-10-08 kinaba: int lines_[] = {1}; b57cc94f1b 2011-10-08 kinaba: vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); b57cc94f1b 2011-10-08 kinaba: int _ = 1; b57cc94f1b 2011-10-08 kinaba: END b57cc94f1b 2011-10-08 kinaba: CASE(5) b57cc94f1b 2011-10-08 kinaba: int lines_[] = {10}; b57cc94f1b 2011-10-08 kinaba: vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); b57cc94f1b 2011-10-08 kinaba: int _ = 10; b57cc94f1b 2011-10-08 kinaba: END b57cc94f1b 2011-10-08 kinaba: CASE(5) b57cc94f1b 2011-10-08 kinaba: int lines_[] = {10,10}; b57cc94f1b 2011-10-08 kinaba: vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); b57cc94f1b 2011-10-08 kinaba: int _ = 11; b57cc94f1b 2011-10-08 kinaba: END b57cc94f1b 2011-10-08 kinaba: CASE(5) b57cc94f1b 2011-10-08 kinaba: int lines_[] = {1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000,1000000}; b57cc94f1b 2011-10-08 kinaba: vector <int> lines(lines_, lines_+sizeof(lines_)/sizeof(*lines_)); b57cc94f1b 2011-10-08 kinaba: int _ = 1000049; b57cc94f1b 2011-10-08 kinaba: END b57cc94f1b 2011-10-08 kinaba: b57cc94f1b 2011-10-08 kinaba: } b57cc94f1b 2011-10-08 kinaba: // END CUT HERE