Skip to content

Instantly share code, notes, and snippets.

@kusano
Created May 24, 2013 13:41
Show Gist options
  • Save kusano/5643590 to your computer and use it in GitHub Desktop.
Save kusano/5643590 to your computer and use it in GitHub Desktop.
// http://community.topcoder.com/stat?c=problem_statement&pm=12324
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
// BEGIN CUT HERE
#include <iostream>
#include <sstream>
#include <string>
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v);
// END CUT HERE
class MarblePositioning{public:
double totalWidth( vector <int> radius )
{
int n = (int)radius.size();
double ans = 1e100;
sort(radius.begin(),radius.end());
do
{
vector<double> p(n);
for ( int i=0; i<n; i++ )
for ( int j=0; j<i; j++ )
{
double d = sqrt(
pow(radius[i]+radius[j],2.) -
pow(radius[i]-radius[j],2.) );
p[i] = max( p[i], p[j]+d );
}
ans = min( ans, p[n-1] );
}
while (next_permutation(radius.begin(),radius.end()));
return ans;
}};
// 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) {
double diff = Expected - Received; if (diff < 0) diff = -diff; bool ok = (diff < 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(_, MarblePositioning().totalWidth(radius));}
int main(){
CASE(0)
int radius_[] = {1, 2};
vector <int> radius(radius_, radius_+sizeof(radius_)/sizeof(*radius_));
double _ = 2.8284271247461903;
END
CASE(1)
int radius_[] = {7,7,7};
vector <int> radius(radius_, radius_+sizeof(radius_)/sizeof(*radius_));
double _ = 28.0;
END
CASE(2)
int radius_[] = {10, 20, 30};
vector <int> radius(radius_, radius_+sizeof(radius_)/sizeof(*radius_));
double _ = 62.92528739883945;
END
CASE(3)
int radius_[] = {100, 100,11,11,25};
vector <int> radius(radius_, radius_+sizeof(radius_)/sizeof(*radius_));
double _ = 200.0;
END
CASE(4)
int radius_[] = {1,999950884,1};
vector <int> radius(radius_, radius_+sizeof(radius_)/sizeof(*radius_));
double _ = 63246.0;
END
}
// END CUT HERE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment