d477545505 2016-01-13 kinaba: #include <iostream> d477545505 2016-01-13 kinaba: #include <sstream> d477545505 2016-01-13 kinaba: #include <iomanip> d477545505 2016-01-13 kinaba: #include <vector> d477545505 2016-01-13 kinaba: #include <string> d477545505 2016-01-13 kinaba: #include <map> d477545505 2016-01-13 kinaba: #include <set> d477545505 2016-01-13 kinaba: #include <algorithm> d477545505 2016-01-13 kinaba: #include <numeric> d477545505 2016-01-13 kinaba: #include <iterator> d477545505 2016-01-13 kinaba: #include <functional> d477545505 2016-01-13 kinaba: #include <complex> d477545505 2016-01-13 kinaba: #include <queue> d477545505 2016-01-13 kinaba: #include <stack> d477545505 2016-01-13 kinaba: #include <cmath> d477545505 2016-01-13 kinaba: #include <cassert> d477545505 2016-01-13 kinaba: #include <tuple> d477545505 2016-01-13 kinaba: using namespace std; d477545505 2016-01-13 kinaba: typedef long long LL; d477545505 2016-01-13 kinaba: typedef complex<double> CMP; d477545505 2016-01-13 kinaba: d477545505 2016-01-13 kinaba: class TheEmpireStrikesBack { public: d477545505 2016-01-13 kinaba: int find(int AX, int BX, int CX, int AY, int BY, int CY, int N, int M) d477545505 2016-01-13 kinaba: { d477545505 2016-01-13 kinaba: vector<pair<int,int>> P(N); d477545505 2016-01-13 kinaba: P[0].first = AX; d477545505 2016-01-13 kinaba: P[0].second = AY; d477545505 2016-01-13 kinaba: for(int i=1; i<N; ++i) { d477545505 2016-01-13 kinaba: P[i].first = int((LL(P[i-1].first) * BX + CX) % 1000000007); d477545505 2016-01-13 kinaba: P[i].second = int((LL(P[i-1].second) * BY + CY) % 1000000007); d477545505 2016-01-13 kinaba: } d477545505 2016-01-13 kinaba: d477545505 2016-01-13 kinaba: // Sort: Large Y first, Large X first d477545505 2016-01-13 kinaba: sort(P.begin(), P.end(), [&](const pair<int,int>& lhs, const pair<int,int>& rhs){ d477545505 2016-01-13 kinaba: if(lhs.second == rhs.second) return lhs.first > rhs.first; d477545505 2016-01-13 kinaba: return lhs.second > rhs.second; d477545505 2016-01-13 kinaba: }); d477545505 2016-01-13 kinaba: d477545505 2016-01-13 kinaba: // Border stars, worth targetting. d477545505 2016-01-13 kinaba: vector<pair<int,int>> B; d477545505 2016-01-13 kinaba: for(auto& p: P) d477545505 2016-01-13 kinaba: if(B.empty() || B.back().first < p.first) d477545505 2016-01-13 kinaba: B.push_back(p); d477545505 2016-01-13 kinaba: d477545505 2016-01-13 kinaba: int L=-1, R=1000000007; // (L,R] d477545505 2016-01-13 kinaba: while(R-L>1) { d477545505 2016-01-13 kinaba: int C = (L+R)/2; d477545505 2016-01-13 kinaba: (least_missile_to_destory_all(B, C)<=M ? R : L) = C; d477545505 2016-01-13 kinaba: } d477545505 2016-01-13 kinaba: return R; d477545505 2016-01-13 kinaba: } d477545505 2016-01-13 kinaba: d477545505 2016-01-13 kinaba: int least_missile_to_destory_all(const vector<pair<int,int>>& B, int T) d477545505 2016-01-13 kinaba: { d477545505 2016-01-13 kinaba: // Greedy. d477545505 2016-01-13 kinaba: int count = 0; d477545505 2016-01-13 kinaba: for(int to_cover=0; to_cover<B.size(); ) { d477545505 2016-01-13 kinaba: int missile = to_cover; d477545505 2016-01-13 kinaba: for(; missile<B.size(); ++missile) d477545505 2016-01-13 kinaba: if(B[missile].first+T < B[to_cover].first || B[missile].second+T < B[to_cover].second) d477545505 2016-01-13 kinaba: break; d477545505 2016-01-13 kinaba: --missile; d477545505 2016-01-13 kinaba: ++count; d477545505 2016-01-13 kinaba: for(; to_cover<B.size(); ++to_cover) d477545505 2016-01-13 kinaba: if(B[missile].first+T < B[to_cover].first || B[missile].second+T < B[to_cover].second) d477545505 2016-01-13 kinaba: break; d477545505 2016-01-13 kinaba: } d477545505 2016-01-13 kinaba: return count; d477545505 2016-01-13 kinaba: } d477545505 2016-01-13 kinaba: }; d477545505 2016-01-13 kinaba: d477545505 2016-01-13 kinaba: // BEGIN CUT HERE d477545505 2016-01-13 kinaba: #include <ctime> d477545505 2016-01-13 kinaba: double start_time; string timer() d477545505 2016-01-13 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } d477545505 2016-01-13 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) d477545505 2016-01-13 kinaba: { os << "{ "; d477545505 2016-01-13 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) d477545505 2016-01-13 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } d477545505 2016-01-13 kinaba: void verify_case(const int& Expected, const int& Received) { d477545505 2016-01-13 kinaba: bool ok = (Expected == Received); d477545505 2016-01-13 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; d477545505 2016-01-13 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } d477545505 2016-01-13 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); d477545505 2016-01-13 kinaba: #define END verify_case(_, TheEmpireStrikesBack().find(AX, BX, CX, AY, BY, CY, N, M));} d477545505 2016-01-13 kinaba: int main(){ d477545505 2016-01-13 kinaba: d477545505 2016-01-13 kinaba: CASE(0) d477545505 2016-01-13 kinaba: int AX = 2; d477545505 2016-01-13 kinaba: int BX = 2; d477545505 2016-01-13 kinaba: int CX = 2; d477545505 2016-01-13 kinaba: int AY = 2; d477545505 2016-01-13 kinaba: int BY = 2; d477545505 2016-01-13 kinaba: int CY = 2; d477545505 2016-01-13 kinaba: int N = 2; d477545505 2016-01-13 kinaba: int M = 1; d477545505 2016-01-13 kinaba: int _ = 0; d477545505 2016-01-13 kinaba: END d477545505 2016-01-13 kinaba: CASE(1) d477545505 2016-01-13 kinaba: int AX = 2; d477545505 2016-01-13 kinaba: int BX = 2; d477545505 2016-01-13 kinaba: int CX = 2; d477545505 2016-01-13 kinaba: int AY = 2; d477545505 2016-01-13 kinaba: int BY = 4; d477545505 2016-01-13 kinaba: int CY = 1000000000; d477545505 2016-01-13 kinaba: int N = 2; d477545505 2016-01-13 kinaba: int M = 1; d477545505 2016-01-13 kinaba: int _ = 1; d477545505 2016-01-13 kinaba: END d477545505 2016-01-13 kinaba: CASE(2) d477545505 2016-01-13 kinaba: int AX = 1; d477545505 2016-01-13 kinaba: int BX = 3; d477545505 2016-01-13 kinaba: int CX = 8; d477545505 2016-01-13 kinaba: int AY = 10000; d477545505 2016-01-13 kinaba: int BY = 10; d477545505 2016-01-13 kinaba: int CY = 999910000; d477545505 2016-01-13 kinaba: int N = 3; d477545505 2016-01-13 kinaba: int M = 1; d477545505 2016-01-13 kinaba: int _ = 30; d477545505 2016-01-13 kinaba: END d477545505 2016-01-13 kinaba: CASE(3) d477545505 2016-01-13 kinaba: int AX = 0; d477545505 2016-01-13 kinaba: int BX = 0; d477545505 2016-01-13 kinaba: int CX = 0; d477545505 2016-01-13 kinaba: int AY = 0; d477545505 2016-01-13 kinaba: int BY = 0; d477545505 2016-01-13 kinaba: int CY = 0; d477545505 2016-01-13 kinaba: int N = 100000; d477545505 2016-01-13 kinaba: int M = 1000; d477545505 2016-01-13 kinaba: int _ = 0; d477545505 2016-01-13 kinaba: END d477545505 2016-01-13 kinaba: CASE(4) d477545505 2016-01-13 kinaba: int AX = 10; d477545505 2016-01-13 kinaba: int BX = 20; d477545505 2016-01-13 kinaba: int CX = 30; d477545505 2016-01-13 kinaba: int AY = 40; d477545505 2016-01-13 kinaba: int BY = 50; d477545505 2016-01-13 kinaba: int CY = 60; d477545505 2016-01-13 kinaba: int N = 100000; d477545505 2016-01-13 kinaba: int M = 10; d477545505 2016-01-13 kinaba: int _ = 15720; d477545505 2016-01-13 kinaba: END d477545505 2016-01-13 kinaba: /* d477545505 2016-01-13 kinaba: CASE(5) d477545505 2016-01-13 kinaba: int AX = ; d477545505 2016-01-13 kinaba: int BX = ; d477545505 2016-01-13 kinaba: int CX = ; d477545505 2016-01-13 kinaba: int AY = ; d477545505 2016-01-13 kinaba: int BY = ; d477545505 2016-01-13 kinaba: int CY = ; d477545505 2016-01-13 kinaba: int N = ; d477545505 2016-01-13 kinaba: int M = ; d477545505 2016-01-13 kinaba: int _ = ; d477545505 2016-01-13 kinaba: END d477545505 2016-01-13 kinaba: CASE(6) d477545505 2016-01-13 kinaba: int AX = ; d477545505 2016-01-13 kinaba: int BX = ; d477545505 2016-01-13 kinaba: int CX = ; d477545505 2016-01-13 kinaba: int AY = ; d477545505 2016-01-13 kinaba: int BY = ; d477545505 2016-01-13 kinaba: int CY = ; d477545505 2016-01-13 kinaba: int N = ; d477545505 2016-01-13 kinaba: int M = ; d477545505 2016-01-13 kinaba: int _ = ; d477545505 2016-01-13 kinaba: END d477545505 2016-01-13 kinaba: */ d477545505 2016-01-13 kinaba: } d477545505 2016-01-13 kinaba: // END CUT HERE