ADDED SRM/678-U/1A.cpp Index: SRM/678-U/1A.cpp ================================================================== --- SRM/678-U/1A.cpp +++ SRM/678-U/1A.cpp @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class ANewHope { public: + int count(vector firstWeek, vector lastWeek, int D) + { + const int N = firstWeek.size(); + const int s = N-D; + int worst = 0; + for(int v=1; v<=N; ++v) { + int a = find(firstWeek.begin(), firstWeek.end(), v) - firstWeek.begin(); + int b = find(lastWeek.begin(), lastWeek.end(), v) - lastWeek.begin(); + worst = max(worst, b==a ? 0 : b>a ? 1 : (a-b+s-1)/s); + } + return worst+1; + } +}; + +// BEGIN CUT HERE +#include +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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(_, ANewHope().count(firstWeek, lastWeek, D));} +int main(){ + +CASE(0) + int firstWeek_[] = {1,2,3,4}; + vector firstWeek(firstWeek_, firstWeek_+sizeof(firstWeek_)/sizeof(*firstWeek_)); + int lastWeek_[] = {4,3,2,1}; + vector lastWeek(lastWeek_, lastWeek_+sizeof(lastWeek_)/sizeof(*lastWeek_)); + int D = 3; + int _ = 4; +END +CASE(1) + int firstWeek_[] = {8,5,4,1,7,6,3,2}; + vector firstWeek(firstWeek_, firstWeek_+sizeof(firstWeek_)/sizeof(*firstWeek_)); + int lastWeek_[] = {2,4,6,8,1,3,5,7}; + vector lastWeek(lastWeek_, lastWeek_+sizeof(lastWeek_)/sizeof(*lastWeek_)); + int D = 3; + int _ = 3; +END +CASE(2) + int firstWeek_[] = {1,2,3,4}; + vector firstWeek(firstWeek_, firstWeek_+sizeof(firstWeek_)/sizeof(*firstWeek_)); + int lastWeek_[] = {1,2,3,4}; + vector lastWeek(lastWeek_, lastWeek_+sizeof(lastWeek_)/sizeof(*lastWeek_)); + int D = 2; + int _ = 1; +END +/* +CASE(3) + int firstWeek_[] = ; + vector firstWeek(firstWeek_, firstWeek_+sizeof(firstWeek_)/sizeof(*firstWeek_)); + int lastWeek_[] = ; + vector lastWeek(lastWeek_, lastWeek_+sizeof(lastWeek_)/sizeof(*lastWeek_)); + int D = ; + int _ = ; +END +CASE(4) + int firstWeek_[] = ; + vector firstWeek(firstWeek_, firstWeek_+sizeof(firstWeek_)/sizeof(*firstWeek_)); + int lastWeek_[] = ; + vector lastWeek(lastWeek_, lastWeek_+sizeof(lastWeek_)/sizeof(*lastWeek_)); + int D = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/678-U/1B.cpp Index: SRM/678-U/1B.cpp ================================================================== --- SRM/678-U/1B.cpp +++ SRM/678-U/1B.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class TheEmpireStrikesBack { public: + int find(int AX, int BX, int CX, int AY, int BY, int CY, int N, int M) + { + vector> P(N); + P[0].first = AX; + P[0].second = AY; + for(int i=1; i& lhs, const pair& rhs){ + if(lhs.second == rhs.second) return lhs.first > rhs.first; + return lhs.second > rhs.second; + }); + + // Border stars, worth targetting. + vector> B; + for(auto& p: P) + if(B.empty() || B.back().first < p.first) + B.push_back(p); + + int L=-1, R=1000000007; // (L,R] + while(R-L>1) { + int C = (L+R)/2; + (least_missile_to_destory_all(B, C)<=M ? R : L) = C; + } + return R; + } + + int least_missile_to_destory_all(const vector>& B, int T) + { + // Greedy. + int count = 0; + for(int to_cover=0; to_cover +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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(_, TheEmpireStrikesBack().find(AX, BX, CX, AY, BY, CY, N, M));} +int main(){ + +CASE(0) + int AX = 2; + int BX = 2; + int CX = 2; + int AY = 2; + int BY = 2; + int CY = 2; + int N = 2; + int M = 1; + int _ = 0; +END +CASE(1) + int AX = 2; + int BX = 2; + int CX = 2; + int AY = 2; + int BY = 4; + int CY = 1000000000; + int N = 2; + int M = 1; + int _ = 1; +END +CASE(2) + int AX = 1; + int BX = 3; + int CX = 8; + int AY = 10000; + int BY = 10; + int CY = 999910000; + int N = 3; + int M = 1; + int _ = 30; +END +CASE(3) + int AX = 0; + int BX = 0; + int CX = 0; + int AY = 0; + int BY = 0; + int CY = 0; + int N = 100000; + int M = 1000; + int _ = 0; +END +CASE(4) + int AX = 10; + int BX = 20; + int CX = 30; + int AY = 40; + int BY = 50; + int CY = 60; + int N = 100000; + int M = 10; + int _ = 15720; +END +/* +CASE(5) + int AX = ; + int BX = ; + int CX = ; + int AY = ; + int BY = ; + int CY = ; + int N = ; + int M = ; + int _ = ; +END +CASE(6) + int AX = ; + int BX = ; + int CX = ; + int AY = ; + int BY = ; + int CY = ; + int N = ; + int M = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/678-U/1C-U.cpp Index: SRM/678-U/1C-U.cpp ================================================================== --- SRM/678-U/1C-U.cpp +++ SRM/678-U/1C-U.cpp @@ -0,0 +1,169 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class ReturnOfTheJedi { public: + vector minimalExpectation(vector x, vector p) + { + vector> item; + for(int i=0; i ans(x.size()); + + vector> iA = item; + double pA = 1.0; + double xA = 0.0; + vector> iB = item; + double pB = 1.0; + double xB = 0.0; + { + vector>::iterator b1 = iB.begin(); + double best_score = 1e+300; + for(vector>::iterator it = iB.begin(); it != iB.end(); ++it) { + double score = it->first * it->second; + if(best_score > score) + {b1 = it, best_score = score;} + } + pB = b1->first; + xB = b1->second; + iB.erase(b1); + ans[0] = (pB*xB); + } + + for(int k=1; k>::iterator b1 = iB.begin(); + double best_score = 1e+300; + for(vector>::iterator it = iB.begin(); it != iB.end(); ++it) { + double score = pB*it->first * (xB+it->second); + if(best_score > score) + {b1 = it, best_score = score;} + } + vector>::iterator b2 = iA.begin(); + vector>::iterator b3 = iA.begin(); + bool A_mode = false; + for(vector>::iterator it = iA.begin(); it != iA.end(); ++it) + for(vector>::iterator kt = it; kt != iA.end(); ++kt) if(kt!=it){ + double score = pA*it->first*kt->first * (xA+it->second+kt->second); + if(best_score > score) + {b2 = it, b3 = kt, best_score = score, A_mode = true;} + } + if(!A_mode) { + pA = pB; + xA = xB; + iA = iB; + pB *= b1->first; + xB += b1->second; + iB.erase(b1); + } else { + pA *= b2->first * b3->first; + xA += b2->second + b3->second; + auto xxx = *b3; + iA.erase(b2); + iA.erase(std::find(iA.begin(), iA.end(), xxx)); + swap(pA, pB); + swap(xA, xB); + swap(iA, iB); + } + ans[k] = pB*xB; + } + return ans; + } +}; + +// BEGIN CUT HERE +#include +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const vector & Expected, const vector & Received) { + bool ok = true; + for(int i=0; i= 1e-9 ) + ok = false; + 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(_, ReturnOfTheJedi().minimalExpectation(x, p));} +int main(){ + +CASE(0) + int x_[] = {100,200,300}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int p_[] = {50000, 20000, 20000}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double __[] = {40.0, 20.000000000000004, 12.000000000000002 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(1) + int x_[] = {200,100,500,300,400}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int p_[] = {100000, 100000, 100000, 100000, 100000}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double __[] = {100.0, 300.0, 600.0, 1000.0, 1500.0 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(2) + int x_[] = {2,2,100,100}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int p_[] = {100000,100000,10000,10000}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double __[] = {2.0, 2.0000000000000004, 2.0200000000000005, 2.0400000000000005 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(3) + int x_[] = {1,1,200,200}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int p_[] = {100000,100000,10000,10000}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double __[] = {1.0, 2.0, 4.010000000000001, 4.0200000000000005 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(4) + int x_[] = {1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000,1000000000}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int p_[] = {90000,90000,90000,90000,90000,90000,90000,90000,90000,90000}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double __[] = {9.0E8, 1.62E9, 2.1870000000000005E9, 2.6244000000000005E9, 2.952450000000001E9, 3.188646000000001E9, 3.348078300000001E9, 3.4437376800000014E9, 3.4867844010000014E9, 3.4867844010000014E9 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +/* +CASE(5) + int x_[] = ; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int p_[] = ; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double __[] = ; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(6) + int x_[] = ; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int p_[] = ; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double __[] = ; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +*/ +} +// END CUT HERE