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