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 BatchSystemRoulette { public: 4fd800b3a8 2011-02-23 kinaba: vector <double> expectedFinishTimes(vector <int> duration, vector <string> user) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<double> answer(duration.begin(), duration.end()); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: map<string, LL> userSum; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<user.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: userSum[user[i]] += duration[i]; 4fd800b3a8 2011-02-23 kinaba: map<LL, vector<string> > usInv; 4fd800b3a8 2011-02-23 kinaba: for(map<string,LL>::iterator it=userSum.begin(); it!=userSum.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: usInv[it->second].push_back(it->first); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double base = 0; 4fd800b3a8 2011-02-23 kinaba: for(map<LL, vector<string> >::iterator it=usInv.begin(); it!=usInv.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL t = it->first; 4fd800b3a8 2011-02-23 kinaba: vector<string>& us = it->second; 4fd800b3a8 2011-02-23 kinaba: int n = us.size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: double baseAve = base + t*(n-1)/2.0; 4fd800b3a8 2011-02-23 kinaba: for(int u=0; u<n; ++u) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<int> idxs; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<answer.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: if( user[i] == us[u] ) { 4fd800b3a8 2011-02-23 kinaba: answer[i] += baseAve; 4fd800b3a8 2011-02-23 kinaba: idxs.push_back(i); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<idxs.size(); ++k) { 4fd800b3a8 2011-02-23 kinaba: int i = idxs[k]; 4fd800b3a8 2011-02-23 kinaba: for(int m=0; m<idxs.size(); ++m) if(m!=k) { 4fd800b3a8 2011-02-23 kinaba: int j = idxs[m]; 4fd800b3a8 2011-02-23 kinaba: answer[i] += duration[j]/2.0; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: base += t*n; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return answer; 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() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const vector <double>& Expected, const vector <double>& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = true; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<Expected.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: if( abs(Expected[i]-Received[i]) >= 1e-9 ) 4fd800b3a8 2011-02-23 kinaba: ok = false; 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, BatchSystemRoulette().expectedFinishTimes(duration, user));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: int duration_[] = {3, 2, 4, 1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> duration(duration_, duration_+sizeof(duration_)/sizeof(*duration_)); 4fd800b3a8 2011-02-23 kinaba: string user_[] = {"Gil Grissom", "Sarah Sidle", "Warrick Brown", "Catherine Willows"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> user(user_, user_+sizeof(user_)/sizeof(*user_)); 4fd800b3a8 2011-02-23 kinaba: double __[] = {6.0, 3.0, 10.0, 1.0 }; 4fd800b3a8 2011-02-23 kinaba: vector <double> _(__, __+sizeof(__)/sizeof(*__)); 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int duration_[] = {3, 2, 4, 1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> duration(duration_, duration_+sizeof(duration_)/sizeof(*duration_)); 4fd800b3a8 2011-02-23 kinaba: string user_[] = {"mac taylor", "Mac Taylor", "Mac Taylor", "Peyton Driscoll"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> user(user_, user_+sizeof(user_)/sizeof(*user_)); 4fd800b3a8 2011-02-23 kinaba: double __[] = {4.0, 8.0, 9.0, 1.0 }; 4fd800b3a8 2011-02-23 kinaba: vector <double> _(__, __+sizeof(__)/sizeof(*__)); 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int duration_[] = {13, 14, 15, 56, 56}; 4fd800b3a8 2011-02-23 kinaba: vector <int> duration(duration_, duration_+sizeof(duration_)/sizeof(*duration_)); 4fd800b3a8 2011-02-23 kinaba: string user_[] = {"Tim Speedle", "Tim Speedle", "Tim Speedle", "Horatio Caine", "YEEEAAAAAAAAAAH"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> user(user_, user_+sizeof(user_)/sizeof(*user_)); 4fd800b3a8 2011-02-23 kinaba: double __[] = {27.5, 28.0, 28.5, 126.0, 126.0 }; 4fd800b3a8 2011-02-23 kinaba: vector <double> _(__, __+sizeof(__)/sizeof(*__)); 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: /* 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: int duration_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> duration(duration_, duration_+sizeof(duration_)/sizeof(*duration_)); 4fd800b3a8 2011-02-23 kinaba: string user_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> user(user_, user_+sizeof(user_)/sizeof(*user_)); 4fd800b3a8 2011-02-23 kinaba: double __[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <double> _(__, __+sizeof(__)/sizeof(*__)); 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: int duration_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> duration(duration_, duration_+sizeof(duration_)/sizeof(*duration_)); 4fd800b3a8 2011-02-23 kinaba: string user_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <string> user(user_, user_+sizeof(user_)/sizeof(*user_)); 4fd800b3a8 2011-02-23 kinaba: double __[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <double> _(__, __+sizeof(__)/sizeof(*__)); 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: */ 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE