4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <cstdlib> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: struct VendingMachine 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int motorUse(vector<string> prices, vector<string> purchases) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<int> sum; 4fd800b3a8 2011-02-23 kinaba: vector< vector<int> > pri(prices.size()); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<prices.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: stringstream sin(prices[i]); 4fd800b3a8 2011-02-23 kinaba: for(int j=0,n; sin>>n; ++j) 4fd800b3a8 2011-02-23 kinaba: {if(j>=sum.size())sum.push_back(0); sum[j]+=n; pri[i].push_back(n);} 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: int M = pri[0].size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int mot = 0; 4fd800b3a8 2011-02-23 kinaba: int pc=0, pt=-5; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<purchases.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: stringstream sin(purchases[i]); 4fd800b3a8 2011-02-23 kinaba: string a; getline(sin,a,','); 4fd800b3a8 2011-02-23 kinaba: string b; getline(sin,b,':'); 4fd800b3a8 2011-02-23 kinaba: string c; getline(sin,c); 4fd800b3a8 2011-02-23 kinaba: int sh = atoi(a.c_str()); 4fd800b3a8 2011-02-23 kinaba: int cl = atoi(b.c_str()); 4fd800b3a8 2011-02-23 kinaba: int t = atoi(c.c_str()); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( pri[sh][cl] == 0 ) 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( t-pt >= 5 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // rotate 4fd800b3a8 2011-02-23 kinaba: int hc = max_element(sum.begin(), sum.end()) - sum.begin(); 4fd800b3a8 2011-02-23 kinaba: mot += min( abs(hc-pc), M-abs(hc-pc) ); 4fd800b3a8 2011-02-23 kinaba: pc = hc; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // buy 4fd800b3a8 2011-02-23 kinaba: sum[cl] -= pri[sh][cl]; 4fd800b3a8 2011-02-23 kinaba: pri[sh][cl] = 0; 4fd800b3a8 2011-02-23 kinaba: mot += min( abs(cl-pc), M-abs(cl-pc) ); 4fd800b3a8 2011-02-23 kinaba: pc = cl; 4fd800b3a8 2011-02-23 kinaba: pt = t; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // rotate 4fd800b3a8 2011-02-23 kinaba: int hc = max_element(sum.begin(), sum.end()) - sum.begin(); 4fd800b3a8 2011-02-23 kinaba: mot += min( abs(hc-pc), M-abs(hc-pc) ); 4fd800b3a8 2011-02-23 kinaba: pc = hc; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return mot; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: };