Artifact Content
Not logged in

Artifact c6752f18484b73a952c7d00dc5dca62088698cba


#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 <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class ArithmeticSequenceDiv1 { public:
	int findMinCost(vector <int> x)
	{
		int best = 0x7fffffff;
		for (int i = 0; i < x.size(); ++i) {
			// x[i]: fixed
			for (int z = 0; z < 1000; ++z) {
				int ss[] = { -1,+1 };
				for (int s : ss) {
					int cur_cost = 0;
					for (int k = 0; k < x.size(); ++k) {
						int v = x[i]+s*z*(k-i);
						cur_cost += abs(v - x[k]);
					}
					best = min(best, cur_cost);
				}
			}
		}
		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 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(_, ArithmeticSequenceDiv1().findMinCost(x));}
int main(){

CASE(0)
	int x_[] = {1,3,2};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int _ = 2; 
END
CASE(1)
	int x_[] = {1,1,1,2,3,4,5};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int _ = 3; 
END
CASE(2)
	int x_[] = {1,2,3,4};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int _ = 0; 
END
CASE(3)
	int x_[] = {1,5,2,5};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int _ = 5; 
END
CASE(4)
	int x_[] = {11,33,22};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int _ = 17; 
END
CASE(5)
	int x_[] = {1, 3, 5, 7, 2, 4, 6};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int _ = 12; 
END
CASE(6)
	int x_[] = {17,68,99,48,66,66,20,88,91,4,20,7,82,23,76,55,50,7,46,88,73,40,41,48,32,18,71,58,56,68,2,99,7,13,19,11,55,95,23,40,17,89,41,12,5,82,93,93,19,20,41,8,15,24,53,87,54,10,12,41,86,95,92,13,56,6,67,72,95,78,82,34,75,12,11,100,99,46,32,45,59,1,63,60,13,94,33,13,85,35,65,40,37,22,37,87,35,2,92,64};
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int _ = -1; 
END
/*
CASE(7)
	int x_[] = ;
	  vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_)); 
	int _ = ; 
END
*/
}
// END CUT HERE