15635fe487 2019-07-06 kinaba: #include <iostream> 15635fe487 2019-07-06 kinaba: #include <sstream> 15635fe487 2019-07-06 kinaba: #include <iomanip> 15635fe487 2019-07-06 kinaba: #include <vector> 15635fe487 2019-07-06 kinaba: #include <string> 15635fe487 2019-07-06 kinaba: #include <map> 15635fe487 2019-07-06 kinaba: #include <set> 15635fe487 2019-07-06 kinaba: #include <algorithm> 15635fe487 2019-07-06 kinaba: #include <numeric> 15635fe487 2019-07-06 kinaba: #include <iterator> 15635fe487 2019-07-06 kinaba: #include <functional> 15635fe487 2019-07-06 kinaba: #include <complex> 15635fe487 2019-07-06 kinaba: #include <queue> 15635fe487 2019-07-06 kinaba: #include <stack> 15635fe487 2019-07-06 kinaba: #include <cmath> 15635fe487 2019-07-06 kinaba: #include <cassert> 15635fe487 2019-07-06 kinaba: #include <tuple> 15635fe487 2019-07-06 kinaba: using namespace std; 15635fe487 2019-07-06 kinaba: typedef long long LL; 15635fe487 2019-07-06 kinaba: typedef complex<double> CMP; 15635fe487 2019-07-06 kinaba: 15635fe487 2019-07-06 kinaba: class MedianFaking { public: 15635fe487 2019-07-06 kinaba: vector <int> minimize(int F, int M, vector <int> data, int goal) 15635fe487 2019-07-06 kinaba: { 15635fe487 2019-07-06 kinaba: vector<pair<int, int>> VP; 15635fe487 2019-07-06 kinaba: for (int p = 0; p < F; ++p) 15635fe487 2019-07-06 kinaba: for (int m = 0; m < M; ++m) 15635fe487 2019-07-06 kinaba: VP.emplace_back(data[p*M + m], p); 15635fe487 2019-07-06 kinaba: sort(VP.begin(), VP.end()); 15635fe487 2019-07-06 kinaba: 15635fe487 2019-07-06 kinaba: const int N = VP.size(); 15635fe487 2019-07-06 kinaba: const int mid = (N - 1) / 2; 15635fe487 2019-07-06 kinaba: 15635fe487 2019-07-06 kinaba: for (int s = 0; s <= N; ++s) { 15635fe487 2019-07-06 kinaba: if (s == N || VP[s].first >= goal) { 15635fe487 2019-07-06 kinaba: for (int m = s; m <= N; ++m) { 15635fe487 2019-07-06 kinaba: if (m == N || VP[m].first > goal) { 15635fe487 2019-07-06 kinaba: // small goal big 15635fe487 2019-07-06 kinaba: // [0,s)[s,m)[m,N) 15635fe487 2019-07-06 kinaba: if (s <= mid && mid < m) 15635fe487 2019-07-06 kinaba: return{ 0,0 }; 15635fe487 2019-07-06 kinaba: if (s > mid) { 15635fe487 2019-07-06 kinaba: return{ choose(s - mid, vector<pair<int, int>>(VP.begin(), VP.begin() + s)), s - mid }; 15635fe487 2019-07-06 kinaba: } 15635fe487 2019-07-06 kinaba: if (m <= mid) { 15635fe487 2019-07-06 kinaba: return{ choose(mid - m + 1, vector<pair<int, int>>(VP.begin() + m, VP.end())), mid - m + 1 }; 15635fe487 2019-07-06 kinaba: } 15635fe487 2019-07-06 kinaba: return{ -1,-1 }; 15635fe487 2019-07-06 kinaba: } 15635fe487 2019-07-06 kinaba: } 15635fe487 2019-07-06 kinaba: break; 15635fe487 2019-07-06 kinaba: } 15635fe487 2019-07-06 kinaba: } 15635fe487 2019-07-06 kinaba: return{ -1,-1 }; 15635fe487 2019-07-06 kinaba: } 15635fe487 2019-07-06 kinaba: 15635fe487 2019-07-06 kinaba: int choose(int k, const vector<pair<int, int>>& VP) 15635fe487 2019-07-06 kinaba: { 15635fe487 2019-07-06 kinaba: map<int, int> pcmap; 15635fe487 2019-07-06 kinaba: for (auto vp : VP) pcmap[vp.second]++; 15635fe487 2019-07-06 kinaba: vector<int> cs; 15635fe487 2019-07-06 kinaba: for (auto pc : pcmap) cs.emplace_back(pc.second); 15635fe487 2019-07-06 kinaba: sort(cs.rbegin(), cs.rend()); 15635fe487 2019-07-06 kinaba: 15635fe487 2019-07-06 kinaba: int p = 0; 15635fe487 2019-07-06 kinaba: for (auto c: cs) { 15635fe487 2019-07-06 kinaba: ++p; 15635fe487 2019-07-06 kinaba: k -= c; 15635fe487 2019-07-06 kinaba: if (k <= 0) break; 15635fe487 2019-07-06 kinaba: } 15635fe487 2019-07-06 kinaba: return p; 15635fe487 2019-07-06 kinaba: } 15635fe487 2019-07-06 kinaba: }; 15635fe487 2019-07-06 kinaba: 15635fe487 2019-07-06 kinaba: // BEGIN CUT HERE 15635fe487 2019-07-06 kinaba: #include <ctime> 15635fe487 2019-07-06 kinaba: double start_time; string timer() 15635fe487 2019-07-06 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 15635fe487 2019-07-06 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 15635fe487 2019-07-06 kinaba: { os << "{ "; 15635fe487 2019-07-06 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 15635fe487 2019-07-06 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 15635fe487 2019-07-06 kinaba: void verify_case(const vector <int>& Expected, const vector <int>& Received) { 15635fe487 2019-07-06 kinaba: bool ok = (Expected == Received); 15635fe487 2019-07-06 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 15635fe487 2019-07-06 kinaba: cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } 15635fe487 2019-07-06 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 15635fe487 2019-07-06 kinaba: #define END verify_case(_, MedianFaking().minimize(F, M, data, goal));} 15635fe487 2019-07-06 kinaba: int main(){ 15635fe487 2019-07-06 kinaba: 15635fe487 2019-07-06 kinaba: CASE(0) 15635fe487 2019-07-06 kinaba: int F = 5; 15635fe487 2019-07-06 kinaba: int M = 5; 15635fe487 2019-07-06 kinaba: int data_[] = {1,2,3,4,5, 10,9,8,7,6, 25,24,23,22,21, 18,16,17,19,20, 11,13,12,14,15}; 15635fe487 2019-07-06 kinaba: vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 15635fe487 2019-07-06 kinaba: int goal = 8; 15635fe487 2019-07-06 kinaba: int __[] = {1, 5 }; 15635fe487 2019-07-06 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 15635fe487 2019-07-06 kinaba: END 15635fe487 2019-07-06 kinaba: CASE(1) 15635fe487 2019-07-06 kinaba: int F = 5; 15635fe487 2019-07-06 kinaba: int M = 5; 15635fe487 2019-07-06 kinaba: int data_[] = {1,2,25,24,23, 3,4,22,21,20, 5,6,19,18,17, 7,16,15,14,13, 8,12,11,10,9}; 15635fe487 2019-07-06 kinaba: vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 15635fe487 2019-07-06 kinaba: int goal = 8; 15635fe487 2019-07-06 kinaba: int __[] = {2, 5 }; 15635fe487 2019-07-06 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 15635fe487 2019-07-06 kinaba: END 15635fe487 2019-07-06 kinaba: CASE(2) 15635fe487 2019-07-06 kinaba: int F = 1; 15635fe487 2019-07-06 kinaba: int M = 4; 15635fe487 2019-07-06 kinaba: int data_[] = {10, 40, 30, 20}; 15635fe487 2019-07-06 kinaba: vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 15635fe487 2019-07-06 kinaba: int goal = 20; 15635fe487 2019-07-06 kinaba: int __[] = {0, 0 }; 15635fe487 2019-07-06 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 15635fe487 2019-07-06 kinaba: END 15635fe487 2019-07-06 kinaba: CASE(3) 15635fe487 2019-07-06 kinaba: int F = 4; 15635fe487 2019-07-06 kinaba: int M = 3; 15635fe487 2019-07-06 kinaba: int data_[] = {3,8,12, 3,8,12, 3,8,12, 8,12,17}; 15635fe487 2019-07-06 kinaba: vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 15635fe487 2019-07-06 kinaba: int goal = 12; 15635fe487 2019-07-06 kinaba: int __[] = {1, 2 }; 15635fe487 2019-07-06 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 15635fe487 2019-07-06 kinaba: END 15635fe487 2019-07-06 kinaba: CASE(4) 15635fe487 2019-07-06 kinaba: int F = 5; 15635fe487 2019-07-06 kinaba: int M = 1; 15635fe487 2019-07-06 kinaba: int data_[] = {10, 20, 30, 40, 50}; 15635fe487 2019-07-06 kinaba: vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 15635fe487 2019-07-06 kinaba: int goal = 23; 15635fe487 2019-07-06 kinaba: int __[] = {1, 1 }; 15635fe487 2019-07-06 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 15635fe487 2019-07-06 kinaba: END 15635fe487 2019-07-06 kinaba: CASE(5) 15635fe487 2019-07-06 kinaba: int F = 1; 15635fe487 2019-07-06 kinaba: int data_[] = { 2 }; 15635fe487 2019-07-06 kinaba: vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 15635fe487 2019-07-06 kinaba: int M = data.size()/F; 15635fe487 2019-07-06 kinaba: int goal = 3; 15635fe487 2019-07-06 kinaba: int __[] = { 1,1 }; 15635fe487 2019-07-06 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 15635fe487 2019-07-06 kinaba: END 15635fe487 2019-07-06 kinaba: /* 15635fe487 2019-07-06 kinaba: CASE(6) 15635fe487 2019-07-06 kinaba: int F = ; 15635fe487 2019-07-06 kinaba: int M = ; 15635fe487 2019-07-06 kinaba: int data_[] = ; 15635fe487 2019-07-06 kinaba: vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 15635fe487 2019-07-06 kinaba: int goal = ; 15635fe487 2019-07-06 kinaba: int __[] = ; 15635fe487 2019-07-06 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 15635fe487 2019-07-06 kinaba: END 15635fe487 2019-07-06 kinaba: */ 15635fe487 2019-07-06 kinaba: } 15635fe487 2019-07-06 kinaba: // END CUT HERE