4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class UnluckyIntervals { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: vector <int> getLuckiest(vector <int> luckySet, int n) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: sort(luckySet.begin(), luckySet.end()); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // zeros 4fd800b3a8 2011-02-23 kinaba: vector<int> luk = luckySet; 4fd800b3a8 2011-02-23 kinaba: for(LL i=0; i+1<luckySet.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: if( i+1 < luckySet.size() ) 4fd800b3a8 2011-02-23 kinaba: if( luckySet[i]+2 == luckySet[i+1] ) 4fd800b3a8 2011-02-23 kinaba: luk.push_back( luckySet[i]+1 ); 4fd800b3a8 2011-02-23 kinaba: if( luckySet[0]==2 ) 4fd800b3a8 2011-02-23 kinaba: luk.push_back(1); 4fd800b3a8 2011-02-23 kinaba: sort(luk.begin(), luk.end()); 4fd800b3a8 2011-02-23 kinaba: if( luk.size() >= n ) 4fd800b3a8 2011-02-23 kinaba: return vector<int>(luk.begin(), luk.begin()+n); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // finites 4fd800b3a8 2011-02-23 kinaba: vector< pair<LL, LL> > luk_val; 4fd800b3a8 2011-02-23 kinaba: LL from = 1; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<luckySet.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL to = luckySet[i]; // [from,to) 4fd800b3a8 2011-02-23 kinaba: if( from+2 <= to ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(LL j=0; j<min<LL>(to-from,n); ++j) { 4fd800b3a8 2011-02-23 kinaba: LL val = (j%2==0 ? from+j/2 : to-1-j/2); 4fd800b3a8 2011-02-23 kinaba: LL unluk = (j/2+1) * (to-from-j/2) - 1; 4fd800b3a8 2011-02-23 kinaba: luk_val.push_back( make_pair(unluk,val) ); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: from = luckySet[i]+1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: sort( luk_val.begin(), luk_val.end() ); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<luk_val.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: luk.push_back(luk_val[i].second); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( luk.size() >= n ) 4fd800b3a8 2011-02-23 kinaba: return vector<int>(luk.begin(), luk.begin()+n); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // infinites 4fd800b3a8 2011-02-23 kinaba: int v = luckySet.back()+1; 4fd800b3a8 2011-02-23 kinaba: while( luk.size() < n ) 4fd800b3a8 2011-02-23 kinaba: luk.push_back(v++); 4fd800b3a8 2011-02-23 kinaba: return luk; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const vector <int> &Expected, const vector <int> &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: int luckySet_[] = {3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> luckySet(luckySet_, luckySet_+sizeof(luckySet_)/sizeof(*luckySet_)); 4fd800b3a8 2011-02-23 kinaba: int n = 6; 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {3, 1, 2, 4, 5, 6 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, UnluckyIntervals().getLuckiest(luckySet, n)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int luckySet_[] = {5, 11, 18}; 4fd800b3a8 2011-02-23 kinaba: vector <int> luckySet(luckySet_, luckySet_+sizeof(luckySet_)/sizeof(*luckySet_)); 4fd800b3a8 2011-02-23 kinaba: int n = 9; 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {5, 11, 18, 1, 4, 6, 10, 2, 3 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, UnluckyIntervals().getLuckiest(luckySet, n)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int luckySet_[] = {7, 13, 18}; 4fd800b3a8 2011-02-23 kinaba: vector <int> luckySet(luckySet_, luckySet_+sizeof(luckySet_)/sizeof(*luckySet_)); 4fd800b3a8 2011-02-23 kinaba: int n = 9; 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {7, 13, 18, 14, 17, 8, 12, 1, 6 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, UnluckyIntervals().getLuckiest(luckySet, n)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int luckySet_[] = {1000, 1004, 4000, 4003, 5000}; 4fd800b3a8 2011-02-23 kinaba: vector <int> luckySet(luckySet_, luckySet_+sizeof(luckySet_)/sizeof(*luckySet_)); 4fd800b3a8 2011-02-23 kinaba: int n = 19; 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {1000, 1004, 4000, 4003, 5000, 4001, 4002, 1001, 1003, 1002, 4004, 4999, 1, 999, 4005, 4998, 2, 998, 4006 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, UnluckyIntervals().getLuckiest(luckySet, n)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int luckySet_[] = {1000000000}; 4fd800b3a8 2011-02-23 kinaba: vector <int> luckySet(luckySet_, luckySet_+sizeof(luckySet_)/sizeof(*luckySet_)); 4fd800b3a8 2011-02-23 kinaba: int n = 8; 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {1000000000, 1, 999999999, 2, 999999998, 3, 999999997, 4 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, UnluckyIntervals().getLuckiest(luckySet, n)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: int luckySet_[] = {2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> luckySet(luckySet_, luckySet_+sizeof(luckySet_)/sizeof(*luckySet_)); 4fd800b3a8 2011-02-23 kinaba: int n = 1; 4fd800b3a8 2011-02-23 kinaba: int RetVal_[] = {1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> RetVal(RetVal_, RetVal_+sizeof(RetVal_)/sizeof(*RetVal_)); 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, UnluckyIntervals().getLuckiest(luckySet, n)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: