Artifact Content
Not logged in

Artifact 27e70b189824397209fadfababb1007c82345954


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

class Deposit { public:
	vector<CMP> make_polygon(const vector<int>& xs, const vector<int>& ys)
	{
		vector<CMP> vs;
		for(int i=0; i<xs.size(); ++i)
			vs.push_back( CMP(xs[i], ys[i]) );
		return vs;
	}

	double successProbability(vector <int> siteX, vector <int> siteY, vector <int> depositX, vector <int> depositY) 
	{
		const vector<CMP> ss = make_polygon(siteX, siteY);
		const vector<CMP> dd = make_polygon(depositX, depositY);
		const int SN = ss.size();

		double totallen=0, hitlen=0;
		for(int i=0; i<SN; ++i)
		{
			const CMP p0 = ss[i];
			const CMP p1 = ss[(i+1)%SN];
			totallen += abs(p1-p0);


			CMP pv = p1 - p0; pv /= abs(pv);

			// calc furthest on p0 - p1
			double q = 1.0;
			for(int k=i+1; k!=i; k=(k+1)%SN)
			{
				const CMP q0 = ss[k];
				const CMP q1 = ss[(k+1)%SN];
				// compute vertical-eqsplitter of lineseg q0 - q1
				CMP mid = (q0+q1)/2.0;
				CMP vec = (q0-q1)*CMP(0,1);;
				// compute crossing with p0 - p1
				double qq = solve(p0,pv,mid,vec);
				if( qq < q )
				{
					if( qq <= 0.0 )
						qq = 0.0;
					// in (qq,q), furthest is q1
					// [todo] compute the fraction of the region crossing with the deposit
					// by ternery search or something like that
					double r = computeFractionOfCross(p0+qq*pv, p0+q*pv, q1, dd);

					hitlen += abs(p0-p1)*(q-qq)*r;
					q = qq;
				}
				if( q == 0.0 )
					break;
			}
		}
		return hitlen / totallen;
	}
	double solve(CMP a, CMP b, CMP c, CMP d)
	{
		// solve: a + <return ?>*b == c + ??*d
		return -1;
	}
	double computeFractionOfCross(CMP p0, CMP p1, CMP q, const vector<CMP>& d)
	{
		// retur the fraction of k in [0,1]  s.t.  line (p0 + k(p1-p0)) to q crosses d.
		return -1;
	}
};

// 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 double& Expected, const double& 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(_, Deposit().successProbability(siteX, siteY, depositX, depositY));}
int main(){

CASE(0)
	int siteX_[] = {0,4,4,0};
	  vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); 
	int siteY_[] = {0,0,4,4};
	  vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); 
	int depositX_[] = {1,2,2,1};
	  vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*depositX_)); 
	int depositY_[] = {1,1,2,2};
	  vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*depositY_)); 
	double _ = 0.6666666666666666; 
END
CASE(1)
	int siteX_[] = {0,4,4,0};
	  vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); 
	int siteY_[] = {0,0,4,4};
	  vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); 
	int depositX_[] = {1,3,3,1};
	  vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*depositX_)); 
	int depositY_[] = {1,1,3,3};
	  vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*depositY_)); 
	double _ = 1.0; 
END
CASE(2)
	int siteX_[] = {5,2,-1,0,4};
	  vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); 
	int siteY_[] = {3,4,2,0,0};
	  vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); 
	int depositX_[] = {3,3,4};
	  vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*depositX_)); 
	int depositY_[] = {3,2,1};
	  vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*depositY_)); 
	double _ = 0.6112700209855423; 
END
CASE(3)
	int siteX_[] = {200,-99,-405,-601,-708,-494,-300,-88};
	  vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); 
	int siteY_[] = {520,516,407,321,104,-97,-221,-101};
	  vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); 
	int depositX_[] = {-101,-201,-296,-400,-402};
	  vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*depositX_)); 
	int depositY_[] = {318,396,402,305,200};
	  vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*depositY_)); 
	double _ = 0.49892756207100747; 
END
CASE(4)
	int siteX_[] = {-3,6,10,10,-2};
	  vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); 
	int siteY_[] = {-1,-10,-9,7,3};
	  vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); 
	int depositX_[] = {3,5,6,4};
	  vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*depositX_)); 
	int depositY_[] = {-5,-7,-6,-4};
	  vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*depositY_)); 
	double _ = 0.16641662251521538; 
END
CASE(5)
	int siteX_[] = {-2,4,-2}
;
	  vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); 
	int siteY_[] = {-2,-2,8}
;
	  vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); 
	int depositX_[] = {-1,0,0}
;
	  vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*depositX_)); 
	int depositY_[] = {0,-1,0}
;
	  vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*depositY_)); 
	double _ = 0.04518850219072291; 
END
/*
CASE(6)
	int siteX_[] = ;
	  vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); 
	int siteY_[] = ;
	  vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); 
	int depositX_[] = ;
	  vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*depositX_)); 
	int depositY_[] = ;
	  vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*depositY_)); 
	double _ = ; 
END
CASE(7)
	int siteX_[] = ;
	  vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); 
	int siteY_[] = ;
	  vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); 
	int depositX_[] = ;
	  vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*depositX_)); 
	int depositY_[] = ;
	  vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*depositY_)); 
	double _ = ; 
END
*/
}
// END CUT HERE