Artifact Content
Not logged in

Artifact 556c57b27a57d85a51eaa5ce2fed02fbd085228e


#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 <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class TowerPlacement { public:
	int solve(int R, int C, int seed, int X)
	{
		vector<int> qr(50000), qc(50000);
		LL state = seed;
		for (int i = 0; i < 50000; ++i) {
			state = (state * 1103515245 + 12345) & 0x7fffffffLL;
			qr[i] = (state / 1000) % R;
			state = (state * 1103515245 + 12345) & 0x7fffffffLL;
			qc[i] = (state / 1000) % C;
		}

		vector<bool> rej(50000);
		for (int r = 0; r < X; ++r) {
			int L = 0, R = 50001;
			while (R - L > 1) {
				int M = (L + R) / 2;
				(feasible(R,C,qr,qc,rej,M) ? L : R) = M;
			}
			if (L == 50000)
				return -1;
			if (r == X - 1)
				return L;
			rej[L] = true;
		}
	}

	// Is placing [0, M) feasible?
	bool feasible(int R, int C, const vector<int>& qr, const vector<int>& qc, const vector<bool>& rej, int M) {
		#define ENC(r,c) ((r)*C+(c))
		map<int, int> t;
		for (int i = 0; i < M; ++i) {
			if (rej[i])
				continue;
			int key = ENC(qr[i], qc[i]);
			if (t.count(key))
				continue;

			t[key] = i;

			// conflict generation
			qr[i]+1, qc[i]
		}
		// solve
		return false;
		#undef ENC
	}
};

// 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(_, TowerPlacement().solve(R, C, seed, X));}
int main(){

CASE(0)
	int R = 1; 
	int C = 1; 
	int seed = 47; 
	int X = 5; 
	int _ = 4; 
END
CASE(1)
	int R = 3; 
	int C = 3; 
	int seed = 47; 
	int X = 4; 
	int _ = 6; 
END
CASE(2)
	int R = 10000; 
	int C = 14700; 
	int seed = 12345; 
	int X = 1; 
	int _ = -1; 
END
CASE(3)
	int R = 10; 
	int C = 12; 
	int seed = 123456789; 
	int X = 3; 
	int _ = 19; 
END
/*
CASE(4)
	int R = ; 
	int C = ; 
	int seed = ; 
	int X = ; 
	int _ = ; 
END
CASE(5)
	int R = ; 
	int C = ; 
	int seed = ; 
	int X = ; 
	int _ = ; 
END
*/
}
// END CUT HERE