Artifact Content
Not logged in

Artifact 3cb93565653b13039dfecc4f3f883a69b1ef2f62


#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>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class DucksAlignment { public:
	int minimumTime(vector <string> grid)
	{
		vector<int> xs, ys;
		for(int y=0; y<grid.size(); ++y)
		for(int x=0; x<grid[y].size(); ++x)
			if( grid[y][x] == 'o' )
			{
				xs.push_back(x);
				ys.push_back(y);
			}
		sort(xs.begin(), xs.end());
		sort(ys.begin(), ys.end());
		return min(single(xs)+line(ys), line(xs)+single(ys));
	}

	int single(const vector<int>& xs)
	{
		int mid = xs[xs.size()/2];
		int sum = 0;
		for(int i=0; i<xs.size(); ++i)
			sum += abs(xs[i] - mid);
		return sum;
	}

	int line(const vector<int>& xs)
	{
		int best = xs.back() * xs.size();
		for(int lb=0; lb<=xs.back(); ++lb) {
			int sum = 0;
			for(int i=0; i<xs.size(); ++i)
				sum += abs(xs[i] - (lb+i));
			best = min(best, sum);
		}
		return best;
	}
};

// 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(_, DucksAlignment().minimumTime(grid));}
int main(){

CASE(0)
	string grid_[] = {".o",
 "o."};
	  vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 
	int _ = 1; 
END
CASE(1)
	string grid_[] = {".o...",
 "..o..",
 "....o"};
	  vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 
	int _ = 3; 
END
CASE(2)
	string grid_[] = {"o..........",
 "..o........",
 ".......o...",
 "...........",
 "...........",
 "...........",
 "........o..",
 "..........."};
	  vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 
	int _ = 16; 
END
CASE(3)
	string grid_[] = {".........",
 "....o....",
 "........."};
	  vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 
	int _ = 0; 
END
CASE(4)
	string grid_[] = {"...o..........................",
 "............................o.",
 ".o............................",
 "............o.................",
 ".................o............",
 "......................o.......",
 "......o.......................",
 "....o.........................",
 "...............o..............",
 ".......................o......",
 "...........................o..",
 ".......o......................"};
	  vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 
	int _ = 99; 
END
/*
CASE(5)
	string grid_[] = ;
	  vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 
	int _ = ; 
END
CASE(6)
	string grid_[] = ;
	  vector <string> grid(grid_, grid_+sizeof(grid_)/sizeof(*grid_)); 
	int _ = ; 
END
*/
}
// END CUT HERE