2573eaa216 2013-03-25 kinaba: #include <iostream> 2573eaa216 2013-03-25 kinaba: #include <sstream> 2573eaa216 2013-03-25 kinaba: #include <iomanip> 2573eaa216 2013-03-25 kinaba: #include <vector> 2573eaa216 2013-03-25 kinaba: #include <string> 2573eaa216 2013-03-25 kinaba: #include <map> 2573eaa216 2013-03-25 kinaba: #include <set> 2573eaa216 2013-03-25 kinaba: #include <algorithm> 2573eaa216 2013-03-25 kinaba: #include <numeric> 2573eaa216 2013-03-25 kinaba: #include <iterator> 2573eaa216 2013-03-25 kinaba: #include <functional> 2573eaa216 2013-03-25 kinaba: #include <complex> 2573eaa216 2013-03-25 kinaba: #include <queue> 2573eaa216 2013-03-25 kinaba: #include <stack> 2573eaa216 2013-03-25 kinaba: #include <cmath> 2573eaa216 2013-03-25 kinaba: #include <cassert> 2573eaa216 2013-03-25 kinaba: using namespace std; 2573eaa216 2013-03-25 kinaba: typedef long long LL; 2573eaa216 2013-03-25 kinaba: typedef long double LD; 2573eaa216 2013-03-25 kinaba: typedef complex<LD> CMP; 2573eaa216 2013-03-25 kinaba: 2573eaa216 2013-03-25 kinaba: class TeamContest { public: 2573eaa216 2013-03-25 kinaba: int worstRank(vector <int> s) 2573eaa216 2013-03-25 kinaba: { 2573eaa216 2013-03-25 kinaba: int N = s.size()/3; 2573eaa216 2013-03-25 kinaba: int us = str(s[0], s[1], s[2]); 2573eaa216 2013-03-25 kinaba: sort(s.begin()+3, s.end()); 2573eaa216 2013-03-25 kinaba: int L = 0; // |<=us| <= 0 is impossible 2573eaa216 2013-03-25 kinaba: int R = N-1; // |<=us| <= N-1 is possible 2573eaa216 2013-03-25 kinaba: while(R-L>1) 2573eaa216 2013-03-25 kinaba: { 2573eaa216 2013-03-25 kinaba: int C = (L+R)/2; 2573eaa216 2013-03-25 kinaba: (possible(us, C, s) ? R : L) = C; 2573eaa216 2013-03-25 kinaba: } 2573eaa216 2013-03-25 kinaba: return N-R; 2573eaa216 2013-03-25 kinaba: } 2573eaa216 2013-03-25 kinaba: 2573eaa216 2013-03-25 kinaba: int str(int a, int b, int c) 2573eaa216 2013-03-25 kinaba: { 2573eaa216 2013-03-25 kinaba: return max(max(a,b),c) + min(min(a,b),c); 2573eaa216 2013-03-25 kinaba: } 2573eaa216 2013-03-25 kinaba: 2573eaa216 2013-03-25 kinaba: // |<=us| <= UB possible? 2573eaa216 2013-03-25 kinaba: bool possible(int us, int UB, const vector<int>& s) 2573eaa216 2013-03-25 kinaba: { 2573eaa216 2013-03-25 kinaba: int si = 3; 2573eaa216 2013-03-25 kinaba: 2573eaa216 2013-03-25 kinaba: // from weak teams 2573eaa216 2013-03-25 kinaba: while(UB > 0) { 2573eaa216 2013-03-25 kinaba: if(si==s.size()) 2573eaa216 2013-03-25 kinaba: return true; 2573eaa216 2013-03-25 kinaba: si += 3; 2573eaa216 2013-03-25 kinaba: --UB; 2573eaa216 2013-03-25 kinaba: } 2573eaa216 2013-03-25 kinaba: 2573eaa216 2013-03-25 kinaba: if(si==s.size()) 2573eaa216 2013-03-25 kinaba: return true; // no more team left 2573eaa216 2013-03-25 kinaba: 2573eaa216 2013-03-25 kinaba: // can we make every other teams stronger than ourselves? 2573eaa216 2013-03-25 kinaba: vector<int> ss(s.begin()+si, s.end()); 2573eaa216 2013-03-25 kinaba: while(!ss.empty()) 2573eaa216 2013-03-25 kinaba: if(!takeOut(us, ss)) 2573eaa216 2013-03-25 kinaba: return false; 2573eaa216 2013-03-25 kinaba: return true; 2573eaa216 2013-03-25 kinaba: } 2573eaa216 2013-03-25 kinaba: 2573eaa216 2013-03-25 kinaba: bool takeOut(int us, vector<int>& s) 2573eaa216 2013-03-25 kinaba: { 2573eaa216 2013-03-25 kinaba: int a = s[0]; 2573eaa216 2013-03-25 kinaba: s.erase(s.begin()); 2573eaa216 2013-03-25 kinaba: int b = s[0]; 2573eaa216 2013-03-25 kinaba: s.erase(s.begin()); 2573eaa216 2013-03-25 kinaba: for(int i=0; i<s.size(); ++i) { 2573eaa216 2013-03-25 kinaba: int c = s[i]; 2573eaa216 2013-03-25 kinaba: if(str(a,b,c) > us) { 2573eaa216 2013-03-25 kinaba: s.erase(s.begin()+i); 2573eaa216 2013-03-25 kinaba: return true; 2573eaa216 2013-03-25 kinaba: } 2573eaa216 2013-03-25 kinaba: } 2573eaa216 2013-03-25 kinaba: return false; 2573eaa216 2013-03-25 kinaba: } 2573eaa216 2013-03-25 kinaba: }; 2573eaa216 2013-03-25 kinaba: 2573eaa216 2013-03-25 kinaba: // BEGIN CUT HERE 2573eaa216 2013-03-25 kinaba: #include <ctime> 2573eaa216 2013-03-25 kinaba: double start_time; string timer() 2573eaa216 2013-03-25 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 2573eaa216 2013-03-25 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 2573eaa216 2013-03-25 kinaba: { os << "{ "; 2573eaa216 2013-03-25 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 2573eaa216 2013-03-25 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 2573eaa216 2013-03-25 kinaba: void verify_case(const int& Expected, const int& Received) { 2573eaa216 2013-03-25 kinaba: bool ok = (Expected == Received); 2573eaa216 2013-03-25 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 2573eaa216 2013-03-25 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 2573eaa216 2013-03-25 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 2573eaa216 2013-03-25 kinaba: #define END verify_case(_, TeamContest().worstRank(strength));} 2573eaa216 2013-03-25 kinaba: int main(){ 2573eaa216 2013-03-25 kinaba: 2573eaa216 2013-03-25 kinaba: CASE(0) 2573eaa216 2013-03-25 kinaba: int strength_[] = {5, 7, 3, 5, 7, 3, 5, 7, 3}; 2573eaa216 2013-03-25 kinaba: vector <int> strength(strength_, strength_+sizeof(strength_)/sizeof(*strength_)); 2573eaa216 2013-03-25 kinaba: int _ = 2; 2573eaa216 2013-03-25 kinaba: END 2573eaa216 2013-03-25 kinaba: CASE(1) 2573eaa216 2013-03-25 kinaba: int strength_[] = {5, 7, 3} 2573eaa216 2013-03-25 kinaba: ; 2573eaa216 2013-03-25 kinaba: vector <int> strength(strength_, strength_+sizeof(strength_)/sizeof(*strength_)); 2573eaa216 2013-03-25 kinaba: int _ = 1; 2573eaa216 2013-03-25 kinaba: END 2573eaa216 2013-03-25 kinaba: CASE(2) 2573eaa216 2013-03-25 kinaba: int strength_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; 2573eaa216 2013-03-25 kinaba: vector <int> strength(strength_, strength_+sizeof(strength_)/sizeof(*strength_)); 2573eaa216 2013-03-25 kinaba: int _ = 1; 2573eaa216 2013-03-25 kinaba: END 2573eaa216 2013-03-25 kinaba: CASE(3) 2573eaa216 2013-03-25 kinaba: int strength_[] = {3,9,4,6,2,6,1,6,9,1,4,1,3,8,5} 2573eaa216 2013-03-25 kinaba: ; 2573eaa216 2013-03-25 kinaba: vector <int> strength(strength_, strength_+sizeof(strength_)/sizeof(*strength_)); 2573eaa216 2013-03-25 kinaba: int _ = 3; 2573eaa216 2013-03-25 kinaba: END 2573eaa216 2013-03-25 kinaba: CASE(4) 2573eaa216 2013-03-25 kinaba: int strength_[] = {53,47,88,79,99,75,28,54,65,14,22,13,11,31,43} 2573eaa216 2013-03-25 kinaba: ; 2573eaa216 2013-03-25 kinaba: vector <int> strength(strength_, strength_+sizeof(strength_)/sizeof(*strength_)); 2573eaa216 2013-03-25 kinaba: int _ = 3; 2573eaa216 2013-03-25 kinaba: END 2573eaa216 2013-03-25 kinaba: CASE(5) 2573eaa216 2013-03-25 kinaba: int strength_[] = {2,2,2,1,1,1,1,1,5}; 2573eaa216 2013-03-25 kinaba: vector <int> strength(strength_, strength_+sizeof(strength_)/sizeof(*strength_)); 2573eaa216 2013-03-25 kinaba: int _ = 2; 2573eaa216 2013-03-25 kinaba: END 2573eaa216 2013-03-25 kinaba: /* 2573eaa216 2013-03-25 kinaba: CASE(6) 2573eaa216 2013-03-25 kinaba: int strength_[] = ; 2573eaa216 2013-03-25 kinaba: vector <int> strength(strength_, strength_+sizeof(strength_)/sizeof(*strength_)); 2573eaa216 2013-03-25 kinaba: int _ = ; 2573eaa216 2013-03-25 kinaba: END 2573eaa216 2013-03-25 kinaba: */ 2573eaa216 2013-03-25 kinaba: } 2573eaa216 2013-03-25 kinaba: // END CUT HERE