69a17ebf8a 2021-07-08 kinaba: #include <iostream> 69a17ebf8a 2021-07-08 kinaba: #include <sstream> 69a17ebf8a 2021-07-08 kinaba: #include <iomanip> 69a17ebf8a 2021-07-08 kinaba: #include <vector> 69a17ebf8a 2021-07-08 kinaba: #include <string> 69a17ebf8a 2021-07-08 kinaba: #include <map> 69a17ebf8a 2021-07-08 kinaba: #include <set> 69a17ebf8a 2021-07-08 kinaba: #include <algorithm> 69a17ebf8a 2021-07-08 kinaba: #include <numeric> 69a17ebf8a 2021-07-08 kinaba: #include <iterator> 69a17ebf8a 2021-07-08 kinaba: #include <functional> 69a17ebf8a 2021-07-08 kinaba: #include <complex> 69a17ebf8a 2021-07-08 kinaba: #include <queue> 69a17ebf8a 2021-07-08 kinaba: #include <stack> 69a17ebf8a 2021-07-08 kinaba: #include <cmath> 69a17ebf8a 2021-07-08 kinaba: #include <cassert> 69a17ebf8a 2021-07-08 kinaba: #include <tuple> 69a17ebf8a 2021-07-08 kinaba: using namespace std; 69a17ebf8a 2021-07-08 kinaba: typedef long long LL; 69a17ebf8a 2021-07-08 kinaba: typedef complex<double> CMP; 69a17ebf8a 2021-07-08 kinaba: 69a17ebf8a 2021-07-08 kinaba: class TowerPlacement { public: 69a17ebf8a 2021-07-08 kinaba: int solve(int R, int C, int seed, int X) 69a17ebf8a 2021-07-08 kinaba: { 69a17ebf8a 2021-07-08 kinaba: vector<int> qr(50000), qc(50000); 69a17ebf8a 2021-07-08 kinaba: LL state = seed; 69a17ebf8a 2021-07-08 kinaba: for (int i = 0; i < 50000; ++i) { 69a17ebf8a 2021-07-08 kinaba: state = (state * 1103515245 + 12345) & 0x7fffffffLL; 69a17ebf8a 2021-07-08 kinaba: qr[i] = (state / 1000) % R; 69a17ebf8a 2021-07-08 kinaba: state = (state * 1103515245 + 12345) & 0x7fffffffLL; 69a17ebf8a 2021-07-08 kinaba: qc[i] = (state / 1000) % C; 69a17ebf8a 2021-07-08 kinaba: } 69a17ebf8a 2021-07-08 kinaba: 69a17ebf8a 2021-07-08 kinaba: vector<bool> rej(50000); 69a17ebf8a 2021-07-08 kinaba: for (int r = 0; r < X; ++r) { 69a17ebf8a 2021-07-08 kinaba: int L = 0, R = 50001; 69a17ebf8a 2021-07-08 kinaba: while (R - L > 1) { 69a17ebf8a 2021-07-08 kinaba: int M = (L + R) / 2; 69a17ebf8a 2021-07-08 kinaba: (feasible(R,C,qr,qc,rej,M) ? L : R) = M; 69a17ebf8a 2021-07-08 kinaba: } 69a17ebf8a 2021-07-08 kinaba: if (L == 50000) 69a17ebf8a 2021-07-08 kinaba: return -1; 69a17ebf8a 2021-07-08 kinaba: if (r == X - 1) 69a17ebf8a 2021-07-08 kinaba: return L; 69a17ebf8a 2021-07-08 kinaba: rej[L] = true; 69a17ebf8a 2021-07-08 kinaba: } 69a17ebf8a 2021-07-08 kinaba: } 69a17ebf8a 2021-07-08 kinaba: 69a17ebf8a 2021-07-08 kinaba: // Is placing [0, M) feasible? 69a17ebf8a 2021-07-08 kinaba: bool feasible(int R, int C, const vector<int>& qr, const vector<int>& qc, const vector<bool>& rej, int M) { 69a17ebf8a 2021-07-08 kinaba: #define ENC(r,c) ((r)*C+(c)) 69a17ebf8a 2021-07-08 kinaba: map<int, int> t; 69a17ebf8a 2021-07-08 kinaba: for (int i = 0; i < M; ++i) { 69a17ebf8a 2021-07-08 kinaba: if (rej[i]) 69a17ebf8a 2021-07-08 kinaba: continue; 69a17ebf8a 2021-07-08 kinaba: int key = ENC(qr[i], qc[i]); 69a17ebf8a 2021-07-08 kinaba: if (t.count(key)) 69a17ebf8a 2021-07-08 kinaba: continue; 69a17ebf8a 2021-07-08 kinaba: 69a17ebf8a 2021-07-08 kinaba: t[key] = i; 69a17ebf8a 2021-07-08 kinaba: 69a17ebf8a 2021-07-08 kinaba: // conflict generation 69a17ebf8a 2021-07-08 kinaba: qr[i]+1, qc[i] 69a17ebf8a 2021-07-08 kinaba: } 69a17ebf8a 2021-07-08 kinaba: // solve 69a17ebf8a 2021-07-08 kinaba: return false; 69a17ebf8a 2021-07-08 kinaba: #undef ENC 69a17ebf8a 2021-07-08 kinaba: } 69a17ebf8a 2021-07-08 kinaba: }; 69a17ebf8a 2021-07-08 kinaba: 69a17ebf8a 2021-07-08 kinaba: // BEGIN CUT HERE 69a17ebf8a 2021-07-08 kinaba: #include <ctime> 69a17ebf8a 2021-07-08 kinaba: double start_time; string timer() 69a17ebf8a 2021-07-08 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 69a17ebf8a 2021-07-08 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 69a17ebf8a 2021-07-08 kinaba: { os << "{ "; 69a17ebf8a 2021-07-08 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 69a17ebf8a 2021-07-08 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 69a17ebf8a 2021-07-08 kinaba: void verify_case(const int& Expected, const int& Received) { 69a17ebf8a 2021-07-08 kinaba: bool ok = (Expected == Received); 69a17ebf8a 2021-07-08 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 69a17ebf8a 2021-07-08 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 69a17ebf8a 2021-07-08 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 69a17ebf8a 2021-07-08 kinaba: #define END verify_case(_, TowerPlacement().solve(R, C, seed, X));} 69a17ebf8a 2021-07-08 kinaba: int main(){ 69a17ebf8a 2021-07-08 kinaba: 69a17ebf8a 2021-07-08 kinaba: CASE(0) 69a17ebf8a 2021-07-08 kinaba: int R = 1; 69a17ebf8a 2021-07-08 kinaba: int C = 1; 69a17ebf8a 2021-07-08 kinaba: int seed = 47; 69a17ebf8a 2021-07-08 kinaba: int X = 5; 69a17ebf8a 2021-07-08 kinaba: int _ = 4; 69a17ebf8a 2021-07-08 kinaba: END 69a17ebf8a 2021-07-08 kinaba: CASE(1) 69a17ebf8a 2021-07-08 kinaba: int R = 3; 69a17ebf8a 2021-07-08 kinaba: int C = 3; 69a17ebf8a 2021-07-08 kinaba: int seed = 47; 69a17ebf8a 2021-07-08 kinaba: int X = 4; 69a17ebf8a 2021-07-08 kinaba: int _ = 6; 69a17ebf8a 2021-07-08 kinaba: END 69a17ebf8a 2021-07-08 kinaba: CASE(2) 69a17ebf8a 2021-07-08 kinaba: int R = 10000; 69a17ebf8a 2021-07-08 kinaba: int C = 14700; 69a17ebf8a 2021-07-08 kinaba: int seed = 12345; 69a17ebf8a 2021-07-08 kinaba: int X = 1; 69a17ebf8a 2021-07-08 kinaba: int _ = -1; 69a17ebf8a 2021-07-08 kinaba: END 69a17ebf8a 2021-07-08 kinaba: CASE(3) 69a17ebf8a 2021-07-08 kinaba: int R = 10; 69a17ebf8a 2021-07-08 kinaba: int C = 12; 69a17ebf8a 2021-07-08 kinaba: int seed = 123456789; 69a17ebf8a 2021-07-08 kinaba: int X = 3; 69a17ebf8a 2021-07-08 kinaba: int _ = 19; 69a17ebf8a 2021-07-08 kinaba: END 69a17ebf8a 2021-07-08 kinaba: /* 69a17ebf8a 2021-07-08 kinaba: CASE(4) 69a17ebf8a 2021-07-08 kinaba: int R = ; 69a17ebf8a 2021-07-08 kinaba: int C = ; 69a17ebf8a 2021-07-08 kinaba: int seed = ; 69a17ebf8a 2021-07-08 kinaba: int X = ; 69a17ebf8a 2021-07-08 kinaba: int _ = ; 69a17ebf8a 2021-07-08 kinaba: END 69a17ebf8a 2021-07-08 kinaba: CASE(5) 69a17ebf8a 2021-07-08 kinaba: int R = ; 69a17ebf8a 2021-07-08 kinaba: int C = ; 69a17ebf8a 2021-07-08 kinaba: int seed = ; 69a17ebf8a 2021-07-08 kinaba: int X = ; 69a17ebf8a 2021-07-08 kinaba: int _ = ; 69a17ebf8a 2021-07-08 kinaba: END 69a17ebf8a 2021-07-08 kinaba: */ 69a17ebf8a 2021-07-08 kinaba: } 69a17ebf8a 2021-07-08 kinaba: // END CUT HERE