Artifact Content
Not logged in

Artifact d73b3908d1719487adf15581e04eeaf5698870cc


#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;

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

class EllysRivers { public:
	double getMin(int length, int walk, vector <int> width, vector <int> speed)
	{
		DP2x<LD> dp(width.size()+1, length+1);
		for(int x=0; x<=length; ++x)
			dp(0,x) = x / LD(walk);

		for(int y=1; y<=width.size(); ++y) {
			LD w = width[y-1];
			LD s = speed[y-1];

			for(int x=length,px=length; x>=0; --x) {
			retry:
				LD t = dp(y-1, px) + sqrt(LD(px-x)*(px-x) + w*w) / s;
				if( px > 0 ) {
					LD tt = dp(y-1, px-1) + sqrt(LD(px-1-x)*(px-1-x) + w*w) / s;
					if( tt < t ) {
						--px;
						goto retry;
					}
				}
				dp(y, x) = t;
			}
		}

		return dp(width.size(), length);
	}
};

// BEGIN CUT HERE
#include <ctime>
LD 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 LD& Expected, const LD& Received) {
 bool ok = (abs(Expected - Received) < 1e-9);
 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(_, EllysRivers().getMin(length, walk, width, speed));}
int main(){

CASE(0)
	int length = 10; 
	int walk = 3; 
	int width_[] = {5, 2, 3};
	  vector <int> width(width_, width_+sizeof(width_)/sizeof(*width_)); 
	int speed_[] = {5, 2, 7};
	  vector <int> speed(speed_, speed_+sizeof(speed_)/sizeof(*speed_)); 
	LD _ = 3.231651964071508; 
END
CASE(1)
	int length = 10000; 
	int walk = 211; 
	int width_[] = {911};
	  vector <int> width(width_, width_+sizeof(width_)/sizeof(*width_)); 
	int speed_[] = {207};
	  vector <int> speed(speed_, speed_+sizeof(speed_)/sizeof(*speed_)); 
	LD _ = 48.24623664712219; 
END
CASE(2)
	int length = 1337; 
	int walk = 2; 
	int width_[] = {100, 200, 300, 400};
	  vector <int> width(width_, width_+sizeof(width_)/sizeof(*width_)); 
	int speed_[] = {11, 12, 13, 14};
	  vector <int> speed(speed_, speed_+sizeof(speed_)/sizeof(*speed_)); 
	LD _ = 128.57830549575695; 
END
CASE(3)
	int length = 77; 
	int walk = 119; 
	int width_[] = {11, 12, 13, 14};
	  vector <int> width(width_, width_+sizeof(width_)/sizeof(*width_)); 
	int speed_[] = {100, 200, 300, 400};
	  vector <int> speed(speed_, speed_+sizeof(speed_)/sizeof(*speed_)); 
	LD _ = 0.3842077071089629; 
END
CASE(4)
	int length = 7134; 
	int walk = 1525; 
	int width_[] = {11567, 19763, 11026, 10444, 24588, 22263, 17709, 11181, 15292, 28895, 15039, 18744, 19985, 13795, 26697, 18812, 25655, 13620, 28926, 12393};
	  vector <int> width(width_, width_+sizeof(width_)/sizeof(*width_)); 
	int speed_[] = {1620, 1477, 2837, 2590, 1692, 2270, 1655, 1078, 2683, 1475, 1383, 1153, 1862, 1770, 1671, 2318, 2197, 1768, 1979, 1057};
	  vector <int> speed(speed_, speed_+sizeof(speed_)/sizeof(*speed_)); 
	LD _ = 214.6509731258811; 
END
CASE(5)
	int length = 100000; 
	int walk = 1; 
	int width_[] = {1000000, 1000000, 1000000};
	  vector <int> width(width_, width_+sizeof(width_)/sizeof(*width_)); 
	int speed_[] = {1, 1000000, 1};
	  vector <int> speed(speed_, speed_+sizeof(speed_)/sizeof(*speed_)); 
	LD _ = 2000001.004987562; 
END
CASE(6)
	int length = 100000; 
	int walk = 1; 
	int width_[] = {1000000, 1000000, 1000000};
	  vector <int> width(width_, width_+sizeof(width_)/sizeof(*width_)); 
	int speed_[] = {1000000, 1000000, 1};
	  vector <int> speed(speed_, speed_+sizeof(speed_)/sizeof(*speed_)); 
	LD _ = 1000002.0024984397; 
END
}
// END CUT HERE