2e5fb9b56a 2013-02-13 kinaba: #include <iostream> 2e5fb9b56a 2013-02-13 kinaba: #include <sstream> 2e5fb9b56a 2013-02-13 kinaba: #include <iomanip> 2e5fb9b56a 2013-02-13 kinaba: #include <vector> 2e5fb9b56a 2013-02-13 kinaba: #include <string> 2e5fb9b56a 2013-02-13 kinaba: #include <map> 2e5fb9b56a 2013-02-13 kinaba: #include <set> 2e5fb9b56a 2013-02-13 kinaba: #include <algorithm> 2e5fb9b56a 2013-02-13 kinaba: #include <numeric> 2e5fb9b56a 2013-02-13 kinaba: #include <iterator> 2e5fb9b56a 2013-02-13 kinaba: #include <functional> 2e5fb9b56a 2013-02-13 kinaba: #include <complex> 2e5fb9b56a 2013-02-13 kinaba: #include <queue> 2e5fb9b56a 2013-02-13 kinaba: #include <stack> 2e5fb9b56a 2013-02-13 kinaba: #include <cmath> 2e5fb9b56a 2013-02-13 kinaba: #include <cassert> 2e5fb9b56a 2013-02-13 kinaba: using namespace std; 2e5fb9b56a 2013-02-13 kinaba: typedef long long LL; 2e5fb9b56a 2013-02-13 kinaba: typedef long double LD; 2e5fb9b56a 2013-02-13 kinaba: typedef complex<LD> CMP; 2e5fb9b56a 2013-02-13 kinaba: 2e5fb9b56a 2013-02-13 kinaba: class TheJediTest { public: 2e5fb9b56a 2013-02-13 kinaba: int minimumSupervisors(vector <int> students, int K) 2e5fb9b56a 2013-02-13 kinaba: { 2e5fb9b56a 2013-02-13 kinaba: return rec(0, 0, students, K); 2e5fb9b56a 2013-02-13 kinaba: } 2e5fb9b56a 2013-02-13 kinaba: 2e5fb9b56a 2013-02-13 kinaba: int rec(int i, int dsi, const vector<int>& s, int K) 2e5fb9b56a 2013-02-13 kinaba: { 2e5fb9b56a 2013-02-13 kinaba: if(i+1 == s.size()) 2e5fb9b56a 2013-02-13 kinaba: return (s[i]+dsi+K-1) / K; 2e5fb9b56a 2013-02-13 kinaba: 2e5fb9b56a 2013-02-13 kinaba: int maxMoveOut = (dsi<0 ? s[i]+dsi : s[i]); 2e5fb9b56a 2013-02-13 kinaba: int maxMoveIn = s[i+1]; 2e5fb9b56a 2013-02-13 kinaba: int X = s[i] + dsi; 2e5fb9b56a 2013-02-13 kinaba: 2e5fb9b56a 2013-02-13 kinaba: int best = 0x7fffffff; 2e5fb9b56a 2013-02-13 kinaba: 2e5fb9b56a 2013-02-13 kinaba: int Xout = X/K*K; 2e5fb9b56a 2013-02-13 kinaba: if(X-Xout > maxMoveOut) 2e5fb9b56a 2013-02-13 kinaba: Xout = X-maxMoveOut; 2e5fb9b56a 2013-02-13 kinaba: best = min(best, (Xout+K-1)/K + rec(i+1, X-Xout, s, K)); 2e5fb9b56a 2013-02-13 kinaba: 2e5fb9b56a 2013-02-13 kinaba: int Xin = (X/K+1)*K; 2e5fb9b56a 2013-02-13 kinaba: if(Xin-X > maxMoveIn) 2e5fb9b56a 2013-02-13 kinaba: Xin = X+maxMoveIn; 2e5fb9b56a 2013-02-13 kinaba: best = min(best, (Xin+K-1)/K + rec(i+1, X-Xin, s, K)); 2e5fb9b56a 2013-02-13 kinaba: return best; 2e5fb9b56a 2013-02-13 kinaba: } 2e5fb9b56a 2013-02-13 kinaba: }; 2e5fb9b56a 2013-02-13 kinaba: 2e5fb9b56a 2013-02-13 kinaba: // BEGIN CUT HERE 2e5fb9b56a 2013-02-13 kinaba: #include <ctime> 2e5fb9b56a 2013-02-13 kinaba: double start_time; string timer() 2e5fb9b56a 2013-02-13 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 2e5fb9b56a 2013-02-13 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 2e5fb9b56a 2013-02-13 kinaba: { os << "{ "; 2e5fb9b56a 2013-02-13 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 2e5fb9b56a 2013-02-13 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 2e5fb9b56a 2013-02-13 kinaba: void verify_case(const int& Expected, const int& Received) { 2e5fb9b56a 2013-02-13 kinaba: bool ok = (Expected == Received); 2e5fb9b56a 2013-02-13 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 2e5fb9b56a 2013-02-13 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 2e5fb9b56a 2013-02-13 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 2e5fb9b56a 2013-02-13 kinaba: #define END verify_case(_, TheJediTest().minimumSupervisors(students, K));} 2e5fb9b56a 2013-02-13 kinaba: int main(){ 2e5fb9b56a 2013-02-13 kinaba: 2e5fb9b56a 2013-02-13 kinaba: CASE(0) 2e5fb9b56a 2013-02-13 kinaba: int students_[] = {3, 6, 3}; 2e5fb9b56a 2013-02-13 kinaba: vector <int> students(students_, students_+sizeof(students_)/sizeof(*students_)); 2e5fb9b56a 2013-02-13 kinaba: int K = 4; 2e5fb9b56a 2013-02-13 kinaba: int _ = 3; 2e5fb9b56a 2013-02-13 kinaba: END 2e5fb9b56a 2013-02-13 kinaba: CASE(1) 2e5fb9b56a 2013-02-13 kinaba: int students_[] = {1, 1, 1, 1}; 2e5fb9b56a 2013-02-13 kinaba: vector <int> students(students_, students_+sizeof(students_)/sizeof(*students_)); 2e5fb9b56a 2013-02-13 kinaba: int K = 4; 2e5fb9b56a 2013-02-13 kinaba: int _ = 2; 2e5fb9b56a 2013-02-13 kinaba: END 2e5fb9b56a 2013-02-13 kinaba: CASE(2) 2e5fb9b56a 2013-02-13 kinaba: int students_[] = {0, 0, 0, 0}; 2e5fb9b56a 2013-02-13 kinaba: vector <int> students(students_, students_+sizeof(students_)/sizeof(*students_)); 2e5fb9b56a 2013-02-13 kinaba: int K = 12345; 2e5fb9b56a 2013-02-13 kinaba: int _ = 0; 2e5fb9b56a 2013-02-13 kinaba: END 2e5fb9b56a 2013-02-13 kinaba: CASE(3) 2e5fb9b56a 2013-02-13 kinaba: int students_[] = {15, 0, 13, 4, 29, 6, 2}; 2e5fb9b56a 2013-02-13 kinaba: vector <int> students(students_, students_+sizeof(students_)/sizeof(*students_)); 2e5fb9b56a 2013-02-13 kinaba: int K = 7; 2e5fb9b56a 2013-02-13 kinaba: int _ = 10; 2e5fb9b56a 2013-02-13 kinaba: END 2e5fb9b56a 2013-02-13 kinaba: CASE(4) 2e5fb9b56a 2013-02-13 kinaba: int students_[] = {1284912, 1009228, 9289247, 2157, 2518, 52781, 2, 2818, 68}; 2e5fb9b56a 2013-02-13 kinaba: vector <int> students(students_, students_+sizeof(students_)/sizeof(*students_)); 2e5fb9b56a 2013-02-13 kinaba: int K = 114; 2e5fb9b56a 2013-02-13 kinaba: int _ = 102138; 2e5fb9b56a 2013-02-13 kinaba: END 2e5fb9b56a 2013-02-13 kinaba: CASE(5) 2e5fb9b56a 2013-02-13 kinaba: int students_[] = {4,0,5}; 2e5fb9b56a 2013-02-13 kinaba: vector <int> students(students_, students_+sizeof(students_)/sizeof(*students_)); 2e5fb9b56a 2013-02-13 kinaba: int K = 3; 2e5fb9b56a 2013-02-13 kinaba: int _ = 3; 2e5fb9b56a 2013-02-13 kinaba: END 2e5fb9b56a 2013-02-13 kinaba: /* 2e5fb9b56a 2013-02-13 kinaba: CASE(6) 2e5fb9b56a 2013-02-13 kinaba: int students_[] = ; 2e5fb9b56a 2013-02-13 kinaba: vector <int> students(students_, students_+sizeof(students_)/sizeof(*students_)); 2e5fb9b56a 2013-02-13 kinaba: int K = ; 2e5fb9b56a 2013-02-13 kinaba: int _ = ; 2e5fb9b56a 2013-02-13 kinaba: END 2e5fb9b56a 2013-02-13 kinaba: */ 2e5fb9b56a 2013-02-13 kinaba: } 2e5fb9b56a 2013-02-13 kinaba: // END CUT HERE