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