Artifact Content
Not logged in

Artifact 5418de6c5fb4301bc59cfc32d02ccda43d72e02e


#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 DrawingLines { public:
	double countLineCrossings(int n, vector <int> startDot, vector <int> endDot) 
	{
		int m = n-startDot.size();
		double news = c(m);
		double ooolds = 0.0;
		for(int i=0; i<startDot.size(); ++i)
			for(int j=i+1; j<startDot.size(); ++j)
				if( startDot[i]<startDot[j] && endDot[i]>endDot[j]
				||  startDot[i]>startDot[j] && endDot[i]<endDot[j] ) 
					ooolds += 1;
		double olds = 0.0;

		vector<int> sTaken, eTaken;
		for(int i=0; i<startDot.size(); ++i) {
			sTaken.push_back(startDot[i]);
			eTaken.push_back(endDot[i]);
		}
		sort(sTaken.begin(), sTaken.end());
		sort(eTaken.begin(), eTaken.end());

		if( n > startDot.size() )
		for(int i=0; i<startDot.size(); ++i) {
			vector<int>::iterator si = lower_bound(sTaken.begin(), sTaken.end(), startDot[i]);
			vector<int>::iterator ei = lower_bound(eTaken.begin(), eTaken.end(),   endDot[i]);
			olds += f( (startDot[i]-1) - (si-sTaken.begin()),
			           (n-startDot[i]) - (sTaken.end()-si-1),
					   (endDot[i]-1) - (ei-eTaken.begin()),
					   (n-endDot[i]) - (eTaken.end()-ei-1)
					   );
		}
		return news + ooolds + olds;
	}

	double c(int m)
	{
		return m*(m-1) / 4.0;
	}

	double f(double a, double b, double c, double d)
	{
		return a * d/(c+d) + b*c/(c+d);
	}
};

// 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(_, DrawingLines().countLineCrossings(n, startDot, endDot));}
int main(){

CASE(0)
	int n = 3; 
	int startDot_[] = {2};
	  vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*startDot_)); 
	int endDot_[] = {3};
	  vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)); 
	double _ = 1.5; 
END
CASE(1)
	int n = 5; 
	int startDot_[] = {1,4};
	  vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*startDot_)); 
	int endDot_[] = {3,1};
	  vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)); 
	double _ = 5.5; 
END
CASE(2)
	int n = 4; 
	int startDot_[] = {4,1};
	  vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*startDot_)); 
	int endDot_[] = {4,1};
	  vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)); 
	double _ = 0.5; 
END
CASE(3)
	int n = 8; 
	int startDot_[] = {1,4,3,6,7};
	  vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*startDot_)); 
	int endDot_[] = {1,3,2,4,5};
	  vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)); 
	double _ = 7.5; 
END
CASE(4)
	int n = 2; 
	int startDot_[] = {1,2} ;
	  vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*startDot_)); 
	int endDot_[] = {1,2};
	  vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)); 
	double _ = 0; 
END
CASE(5)
	int n = 2; 
	int startDot_[] = {1,2} ;
	  vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*startDot_)); 
	int endDot_[] = {2,1};
	  vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)); 
	double _ = 1; 
END
CASE(6)
	int n = 3; 
	int startDot_[] = {1} ;
	  vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*startDot_)); 
	int endDot_[] = {1};
	  vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)); 
	double _ = 0.5; 
END
CASE(7)
	int n = 3; 
	int startDot_[] = {1} ;
	  vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*startDot_)); 
	int endDot_[] = {3};
	  vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)); 
	double _ = 2.5; 
END
CASE(8)
	int n = 10000; 
	int startDot_[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
	  vector <int> startDot(startDot_, startDot_+sizeof(startDot_)/sizeof(*startDot_)); 
	  int endDot_[] = {20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
	  vector <int> endDot(endDot_, endDot_+sizeof(endDot_)/sizeof(*endDot_)); 
	double _ = -1; 
END

}
// END CUT HERE