4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class Deposit { public: 4fd800b3a8 2011-02-23 kinaba: vector<CMP> make_polygon(const vector<int>& xs, const vector<int>& ys) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<CMP> vs; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<xs.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: vs.push_back( CMP(xs[i], ys[i]) ); 4fd800b3a8 2011-02-23 kinaba: return vs; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double successProbability(vector <int> siteX, vector <int> siteY, vector <int> depositX, vector <int> depositY) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: const vector<CMP> ss = make_polygon(siteX, siteY); 4fd800b3a8 2011-02-23 kinaba: const vector<CMP> dd = make_polygon(depositX, depositY); 4fd800b3a8 2011-02-23 kinaba: const int SN = ss.size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double totallen=0, hitlen=0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<SN; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: const CMP p0 = ss[i]; 4fd800b3a8 2011-02-23 kinaba: const CMP p1 = ss[(i+1)%SN]; 4fd800b3a8 2011-02-23 kinaba: totallen += abs(p1-p0); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CMP pv = p1 - p0; pv /= abs(pv); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // calc furthest on p0 - p1 4fd800b3a8 2011-02-23 kinaba: double q = 1.0; 4fd800b3a8 2011-02-23 kinaba: for(int k=i+1; k!=i; k=(k+1)%SN) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: const CMP q0 = ss[k]; 4fd800b3a8 2011-02-23 kinaba: const CMP q1 = ss[(k+1)%SN]; 4fd800b3a8 2011-02-23 kinaba: // compute vertical-eqsplitter of lineseg q0 - q1 4fd800b3a8 2011-02-23 kinaba: CMP mid = (q0+q1)/2.0; 4fd800b3a8 2011-02-23 kinaba: CMP vec = (q0-q1)*CMP(0,1);; 4fd800b3a8 2011-02-23 kinaba: // compute crossing with p0 - p1 4fd800b3a8 2011-02-23 kinaba: double qq = solve(p0,pv,mid,vec); 4fd800b3a8 2011-02-23 kinaba: if( qq < q ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( qq <= 0.0 ) 4fd800b3a8 2011-02-23 kinaba: qq = 0.0; 4fd800b3a8 2011-02-23 kinaba: // in (qq,q), furthest is q1 4fd800b3a8 2011-02-23 kinaba: // [todo] compute the fraction of the region crossing with the deposit 4fd800b3a8 2011-02-23 kinaba: // by ternery search or something like that 4fd800b3a8 2011-02-23 kinaba: double r = computeFractionOfCross(p0+qq*pv, p0+q*pv, q1, dd); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: hitlen += abs(p0-p1)*(q-qq)*r; 4fd800b3a8 2011-02-23 kinaba: q = qq; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( q == 0.0 ) 4fd800b3a8 2011-02-23 kinaba: break; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return hitlen / totallen; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: double solve(CMP a, CMP b, CMP c, CMP d) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // solve: a + <return ?>*b == c + ??*d 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: double computeFractionOfCross(CMP p0, CMP p1, CMP q, const vector<CMP>& d) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // retur the fraction of k in [0,1] s.t. line (p0 + k(p1-p0)) to q crosses d. 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const double& Expected, const double& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (abs(Expected - Received) < 1e-9); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, Deposit().successProbability(siteX, siteY, depositX, depositY));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: int siteX_[] = {0,4,4,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); 4fd800b3a8 2011-02-23 kinaba: int siteY_[] = {0,0,4,4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); 4fd800b3a8 2011-02-23 kinaba: int depositX_[] = {1,2,2,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*depositX_)); 4fd800b3a8 2011-02-23 kinaba: int depositY_[] = {1,1,2,2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*depositY_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 0.6666666666666666; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int siteX_[] = {0,4,4,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); 4fd800b3a8 2011-02-23 kinaba: int siteY_[] = {0,0,4,4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); 4fd800b3a8 2011-02-23 kinaba: int depositX_[] = {1,3,3,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*depositX_)); 4fd800b3a8 2011-02-23 kinaba: int depositY_[] = {1,1,3,3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*depositY_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 1.0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int siteX_[] = {5,2,-1,0,4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); 4fd800b3a8 2011-02-23 kinaba: int siteY_[] = {3,4,2,0,0}; 4fd800b3a8 2011-02-23 kinaba: vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); 4fd800b3a8 2011-02-23 kinaba: int depositX_[] = {3,3,4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*depositX_)); 4fd800b3a8 2011-02-23 kinaba: int depositY_[] = {3,2,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*depositY_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 0.6112700209855423; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: int siteX_[] = {200,-99,-405,-601,-708,-494,-300,-88}; 4fd800b3a8 2011-02-23 kinaba: vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); 4fd800b3a8 2011-02-23 kinaba: int siteY_[] = {520,516,407,321,104,-97,-221,-101}; 4fd800b3a8 2011-02-23 kinaba: vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); 4fd800b3a8 2011-02-23 kinaba: int depositX_[] = {-101,-201,-296,-400,-402}; 4fd800b3a8 2011-02-23 kinaba: vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*depositX_)); 4fd800b3a8 2011-02-23 kinaba: int depositY_[] = {318,396,402,305,200}; 4fd800b3a8 2011-02-23 kinaba: vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*depositY_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 0.49892756207100747; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: int siteX_[] = {-3,6,10,10,-2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); 4fd800b3a8 2011-02-23 kinaba: int siteY_[] = {-1,-10,-9,7,3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); 4fd800b3a8 2011-02-23 kinaba: int depositX_[] = {3,5,6,4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*depositX_)); 4fd800b3a8 2011-02-23 kinaba: int depositY_[] = {-5,-7,-6,-4}; 4fd800b3a8 2011-02-23 kinaba: vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*depositY_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 0.16641662251521538; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int siteX_[] = {-2,4,-2} 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); 4fd800b3a8 2011-02-23 kinaba: int siteY_[] = {-2,-2,8} 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); 4fd800b3a8 2011-02-23 kinaba: int depositX_[] = {-1,0,0} 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*depositX_)); 4fd800b3a8 2011-02-23 kinaba: int depositY_[] = {0,-1,0} 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*depositY_)); 4fd800b3a8 2011-02-23 kinaba: double _ = 0.04518850219072291; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: /* 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: int siteX_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); 4fd800b3a8 2011-02-23 kinaba: int siteY_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); 4fd800b3a8 2011-02-23 kinaba: int depositX_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*depositX_)); 4fd800b3a8 2011-02-23 kinaba: int depositY_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*depositY_)); 4fd800b3a8 2011-02-23 kinaba: double _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(7) 4fd800b3a8 2011-02-23 kinaba: int siteX_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> siteX(siteX_, siteX_+sizeof(siteX_)/sizeof(*siteX_)); 4fd800b3a8 2011-02-23 kinaba: int siteY_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> siteY(siteY_, siteY_+sizeof(siteY_)/sizeof(*siteY_)); 4fd800b3a8 2011-02-23 kinaba: int depositX_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> depositX(depositX_, depositX_+sizeof(depositX_)/sizeof(*depositX_)); 4fd800b3a8 2011-02-23 kinaba: int depositY_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> depositY(depositY_, depositY_+sizeof(depositY_)/sizeof(*depositY_)); 4fd800b3a8 2011-02-23 kinaba: double _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: */ 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE