Artifact Content
Not logged in

Artifact 7efed63f7388becb42b02f6d2d352aae50bf23ec


#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>
#include <cstring>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

template<typename T>
struct DP3
{
	int N1, N2, N3;
	vector<T> data;
	DP3(int N1, int N2, int N3, const T& t = T())
		: N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()*sizeof(T)<(1<<26)); }
	T& operator()(int i1, int i2, int i3)
		{ return data[ ((i1*N2)+i2)*N3+i3 ]; }
	void swap(DP3& rhs)
		{ data.swap(rhs.data); }
};

class CucumberWatering { public:
	long long theMin(vector <int> x, int K)
	{
		const int N = x.size();

		vector<pair<LL,int> > pts;
		for(int i=0; i<N; ++i)
			pts.push_back(make_pair(x[i], i));
		sort(pts.begin(), pts.end());

		DP3<LL> dp(N, K+1, N, -12345678987654321LL);
		dp(0, 0, 0) = dp(0, 1, 0) = 0;
		for(int i=1; i<N; ++i)
			for(int warpUsed=0; warpUsed<=K; ++warpUsed) {
				for(int lastWarpPt=0; lastWarpPt<i; ++lastWarpPt)
					dp(i,warpUsed,lastWarpPt) = dp(i-1,warpUsed,lastWarpPt);

				LL maxGain = 0;
				if( warpUsed > 1 )
					for(int p=0; p<i; ++p) {
						LL gain = 0;
						LL P = pts[p].first;
						LL Q = pts[i].first;
						for(int k=1; k<N; ++k) {
							LL X = min(x[k-1], x[k]);
							LL Y = max(x[k-1], x[k]);
							gain += max(0LL, (Y-X) - (abs(X-P) + abs(Y-Q)));
						}
						maxGain = max(maxGain, dp(i,warpUsed-1,p) + gain);
					}
				dp(i,warpUsed,i) = maxGain;
			}

		LL naive = 0;
		for(int i=1; i<N; ++i)
			naive += abs(LL(x[i-1]) - LL(x[i]));

		LL maxGain = 0;
		for(int w=0; w<=K; ++w)
			for(int p=0; p<N; ++p)
				maxGain = max(maxGain, dp(N-1,w,p));

		return naive - maxGain;
	}
};

// 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 long long& Expected, const long long& 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(_, CucumberWatering().theMin(x, K));}
int main(){

CASE(0)
	int x_[] = {0, 6, 8, 2};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int K = 2; 
	long long _ = 6LL; 
END
CASE(1)
	int x_[] = {-1000000000, 1000000000, 0};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int K = 1; 
	long long _ = 3000000000LL; 
END
CASE(2)
	int x_[] = {58, 2012};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int K = 50; 
	long long _ = 0LL; 
END
CASE(3)
	int x_[] = {9, -3, 14, 6, 5, -9, 32, 7, -5, 26, 2, 11};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int K = 3; 
	long long _ = 58LL; 
END
/*
CASE(4)
	int x_[] = ;
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int K = ; 
	long long _ = LL; 
END
CASE(5)
	int x_[] = ;
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int K = ; 
	long long _ = LL; 
END
*/
}
// END CUT HERE