Artifact 894982b1719fd8db73875a71fea69dbe8222d9b1
#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;
struct UnionFind
{
vector<int> uf, sz;
int nc;
UnionFind(int N): uf(N), sz(N,1), nc(N)
{ for(int i=0; i<N; ++i) uf[i] = i; }
int size()
{ return nc; }
int Find(int a)
{ return uf[a]==a ? a : uf[a]=Find(uf[a]); }
bool Union(int a, int b)
{
a = Find(a);
b = Find(b);
if( a != b )
{
if( sz[a] >= sz[b] ) swap(a, b);
uf[a] = b;
sz[b] += sz[a];
--nc;
}
return (a!=b);
}
};
class CrazyLine { public:
int maxCrazyness(vector <int> heights)
{
typedef pair< int, pair<int,int> > cand;
priority_queue<cand> Q;
for(int i=0; i<heights.size(); ++i)
for(int j=i+1; j<heights.size(); ++j)
Q.push( cand(abs(heights[i]-heights[j]), make_pair(i,j)) );
UnionFind uf(heights.size());
int score=0;
vector<int> used(heights.size());
while( uf.size()>1 )
{
int s = Q.top().first;
int i = Q.top().second.first;
int j = Q.top().second.second;
Q.pop();
if( used[i]<=1 && used[j]<=1 && uf.Find(i)!=uf.Find(j) )
{
score += s;
used[i]++;
used[j]++;
uf.Union(i,j);
}
}
return score;
}
};
// 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 int& Expected, const int& Received) {
bool ok = (Expected == Received);
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(_, CrazyLine().maxCrazyness(heights));}
int main(){
CASE(0)
int heights_[] = {5,10,25,40,25};
vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_));
int _ = 100;
END
CASE(1)
int heights_[] = {2,3,5,7,11,13,17,19};
vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_));
int _ = 82;
END
CASE(2)
int heights_[] = {1,1,1,1,1,1,501};
vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_));
int _ = 1000;
END
CASE(3)
int heights_[] = {1000,1000,1000,1000,1000,1000,1000,1000,1000};
vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_));
int _ = 0;
END
/*
CASE(4)
int heights_[] = ;
vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_));
int _ = ;
END
CASE(5)
int heights_[] = ;
vector <int> heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_));
int _ = ;
END
*/
}
// END CUT HERE