0b26cb3f9a 2012-05-29 kinaba: #include <iostream> 0b26cb3f9a 2012-05-29 kinaba: #include <sstream> 0b26cb3f9a 2012-05-29 kinaba: #include <iomanip> 0b26cb3f9a 2012-05-29 kinaba: #include <vector> 0b26cb3f9a 2012-05-29 kinaba: #include <string> 0b26cb3f9a 2012-05-29 kinaba: #include <map> 0b26cb3f9a 2012-05-29 kinaba: #include <set> 0b26cb3f9a 2012-05-29 kinaba: #include <algorithm> 0b26cb3f9a 2012-05-29 kinaba: #include <numeric> 0b26cb3f9a 2012-05-29 kinaba: #include <iterator> 0b26cb3f9a 2012-05-29 kinaba: #include <functional> 0b26cb3f9a 2012-05-29 kinaba: #include <complex> 0b26cb3f9a 2012-05-29 kinaba: #include <queue> 0b26cb3f9a 2012-05-29 kinaba: #include <stack> 0b26cb3f9a 2012-05-29 kinaba: #include <cmath> 0b26cb3f9a 2012-05-29 kinaba: #include <cassert> 0b26cb3f9a 2012-05-29 kinaba: using namespace std; 0b26cb3f9a 2012-05-29 kinaba: typedef long long LL; 0b26cb3f9a 2012-05-29 kinaba: typedef long double LD; 0b26cb3f9a 2012-05-29 kinaba: typedef complex<LD> CMP; 0b26cb3f9a 2012-05-29 kinaba: 0b26cb3f9a 2012-05-29 kinaba: class ElectionFraudDiv1 { public: 0b26cb3f9a 2012-05-29 kinaba: int MinimumVoters(vector <int> percentages) 0b26cb3f9a 2012-05-29 kinaba: { 0b26cb3f9a 2012-05-29 kinaba: for(int voter=1; voter<=100000; ++voter) 0b26cb3f9a 2012-05-29 kinaba: if(possible(voter, percentages)) 0b26cb3f9a 2012-05-29 kinaba: return voter; 0b26cb3f9a 2012-05-29 kinaba: return -1; 0b26cb3f9a 2012-05-29 kinaba: } 0b26cb3f9a 2012-05-29 kinaba: 0b26cb3f9a 2012-05-29 kinaba: bool possible(int V, const vector<int>& P) 0b26cb3f9a 2012-05-29 kinaba: { 0b26cb3f9a 2012-05-29 kinaba: int L=0, R=0; 0b26cb3f9a 2012-05-29 kinaba: for(int i=0; i<P.size(); ++i) 0b26cb3f9a 2012-05-29 kinaba: { 0b26cb3f9a 2012-05-29 kinaba: int p = P[i]; 0b26cb3f9a 2012-05-29 kinaba: 0b26cb3f9a 2012-05-29 kinaba: // p-0.5 <= x/V*100 < p+0.5 0b26cb3f9a 2012-05-29 kinaba: // 2p-1 <= x/V*200 < 2p+1 0b26cb3f9a 2012-05-29 kinaba: 0b26cb3f9a 2012-05-29 kinaba: int ll = (2*p-1)*V; 0b26cb3f9a 2012-05-29 kinaba: int rr = (2*p+1)*V; 0b26cb3f9a 2012-05-29 kinaba: 0b26cb3f9a 2012-05-29 kinaba: // ll <= x*200 < rr 0b26cb3f9a 2012-05-29 kinaba: 0b26cb3f9a 2012-05-29 kinaba: int l = max(0, (ll+199)/200); 0b26cb3f9a 2012-05-29 kinaba: int r = min(V, rr/200 - (rr%200 ? 0 : 1)); 0b26cb3f9a 2012-05-29 kinaba: 0b26cb3f9a 2012-05-29 kinaba: // l <= x <= r 0b26cb3f9a 2012-05-29 kinaba: 0b26cb3f9a 2012-05-29 kinaba: if( l <= r ) 0b26cb3f9a 2012-05-29 kinaba: L+=l, R+=r; 0b26cb3f9a 2012-05-29 kinaba: else 0b26cb3f9a 2012-05-29 kinaba: return false; 0b26cb3f9a 2012-05-29 kinaba: } 0b26cb3f9a 2012-05-29 kinaba: return L<=V && V<=R; 0b26cb3f9a 2012-05-29 kinaba: } 0b26cb3f9a 2012-05-29 kinaba: }; 0b26cb3f9a 2012-05-29 kinaba: 0b26cb3f9a 2012-05-29 kinaba: // BEGIN CUT HERE 0b26cb3f9a 2012-05-29 kinaba: #include <ctime> 0b26cb3f9a 2012-05-29 kinaba: double start_time; string timer() 0b26cb3f9a 2012-05-29 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 0b26cb3f9a 2012-05-29 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 0b26cb3f9a 2012-05-29 kinaba: { os << "{ "; 0b26cb3f9a 2012-05-29 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 0b26cb3f9a 2012-05-29 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 0b26cb3f9a 2012-05-29 kinaba: void verify_case(const int& Expected, const int& Received) { 0b26cb3f9a 2012-05-29 kinaba: bool ok = (Expected == Received); 0b26cb3f9a 2012-05-29 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 0b26cb3f9a 2012-05-29 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 0b26cb3f9a 2012-05-29 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 0b26cb3f9a 2012-05-29 kinaba: #define END verify_case(_, ElectionFraudDiv1().MinimumVoters(percentages));} 0b26cb3f9a 2012-05-29 kinaba: int main(){ 0b26cb3f9a 2012-05-29 kinaba: 0b26cb3f9a 2012-05-29 kinaba: CASE(0) 0b26cb3f9a 2012-05-29 kinaba: int percentages_[] = {33, 33, 33}; 0b26cb3f9a 2012-05-29 kinaba: vector <int> percentages(percentages_, percentages_+sizeof(percentages_)/sizeof(*percentages_)); 0b26cb3f9a 2012-05-29 kinaba: int _ = 3; 0b26cb3f9a 2012-05-29 kinaba: END 0b26cb3f9a 2012-05-29 kinaba: CASE(1) 0b26cb3f9a 2012-05-29 kinaba: int percentages_[] = {29, 29, 43}; 0b26cb3f9a 2012-05-29 kinaba: vector <int> percentages(percentages_, percentages_+sizeof(percentages_)/sizeof(*percentages_)); 0b26cb3f9a 2012-05-29 kinaba: int _ = 7; 0b26cb3f9a 2012-05-29 kinaba: END 0b26cb3f9a 2012-05-29 kinaba: CASE(2) 0b26cb3f9a 2012-05-29 kinaba: int percentages_[] = {12, 12, 12, 12, 12, 12, 12, 12}; 0b26cb3f9a 2012-05-29 kinaba: vector <int> percentages(percentages_, percentages_+sizeof(percentages_)/sizeof(*percentages_)); 0b26cb3f9a 2012-05-29 kinaba: int _ = -1; 0b26cb3f9a 2012-05-29 kinaba: END 0b26cb3f9a 2012-05-29 kinaba: CASE(3) 0b26cb3f9a 2012-05-29 kinaba: int percentages_[] = {13, 13, 13, 13, 13, 13, 13, 13}; 0b26cb3f9a 2012-05-29 kinaba: vector <int> percentages(percentages_, percentages_+sizeof(percentages_)/sizeof(*percentages_)); 0b26cb3f9a 2012-05-29 kinaba: int _ = 8; 0b26cb3f9a 2012-05-29 kinaba: END 0b26cb3f9a 2012-05-29 kinaba: CASE(4) 0b26cb3f9a 2012-05-29 kinaba: int percentages_[] = {0, 1, 100}; 0b26cb3f9a 2012-05-29 kinaba: vector <int> percentages(percentages_, percentages_+sizeof(percentages_)/sizeof(*percentages_)); 0b26cb3f9a 2012-05-29 kinaba: int _ = 200; 0b26cb3f9a 2012-05-29 kinaba: END 0b26cb3f9a 2012-05-29 kinaba: CASE(5) 0b26cb3f9a 2012-05-29 kinaba: int percentages_[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4}; 0b26cb3f9a 2012-05-29 kinaba: vector <int> percentages(percentages_, percentages_+sizeof(percentages_)/sizeof(*percentages_)); 0b26cb3f9a 2012-05-29 kinaba: int _ = 97; 0b26cb3f9a 2012-05-29 kinaba: END 0b26cb3f9a 2012-05-29 kinaba: /* 0b26cb3f9a 2012-05-29 kinaba: CASE(6) 0b26cb3f9a 2012-05-29 kinaba: int percentages_[] = ; 0b26cb3f9a 2012-05-29 kinaba: vector <int> percentages(percentages_, percentages_+sizeof(percentages_)/sizeof(*percentages_)); 0b26cb3f9a 2012-05-29 kinaba: int _ = ; 0b26cb3f9a 2012-05-29 kinaba: END 0b26cb3f9a 2012-05-29 kinaba: CASE(7) 0b26cb3f9a 2012-05-29 kinaba: int percentages_[] = ; 0b26cb3f9a 2012-05-29 kinaba: vector <int> percentages(percentages_, percentages_+sizeof(percentages_)/sizeof(*percentages_)); 0b26cb3f9a 2012-05-29 kinaba: int _ = ; 0b26cb3f9a 2012-05-29 kinaba: END 0b26cb3f9a 2012-05-29 kinaba: */ 0b26cb3f9a 2012-05-29 kinaba: } 0b26cb3f9a 2012-05-29 kinaba: // END CUT HERE