Skip to content

Instantly share code, notes, and snippets.

@diogot
Last active December 19, 2015 06:58
Show Gist options
  • Save diogot/5914723 to your computer and use it in GitHub Desktop.
Save diogot/5914723 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int nextAvailable(unsigned int C, int *chashier)
{
int avail = 0;
int min = chashier[0];
for (int i=1; i<C; ++i) {
if (chashier[i]<min) {
min = chashier[i];
avail = i;
}
}
return avail;
}
int main(int argc, const char *argv[])
{
string inFileName;
switch (argc) {
case 2:
inFileName = argv[1];
break;
default:
cout << "Usage: " << argv[0] << " inputFile" << endl;
exit(0);
break;
}
fstream inFile(inFileName.c_str(),fstream::in);
if (!inFile.is_open()) {
cout << "File " << inFileName << " is not open!" << endl;
exit(1);
}
unsigned int C = 0; // number of cashiers
unsigned int N = 0; // number of clients
int maxWaiting = 20;
int clientsOverTime = 0;
if(!(inFile >> C >> N)){
cout << "Error, can't read C and N!" << endl;
exit(1);
}
int *cashier = new int [C]; // time when the cashier is free
for (int i=0; i<C; ++i)
cashier[i] = 0;
for (unsigned int iN = 0; iN < N; ++iN) {
int T = 0; // time that the client arrived on the line
int D = 0; // attendance time
if(!(inFile >> T >> D)){
cout << "Error, can't read T or D!" << endl;
exit(1);
}
int avail = nextAvailable(C, cashier);
int waitTime = cashier[avail] - T;
if (waitTime > maxWaiting)
++clientsOverTime;
cashier[avail] = (waitTime > 0) ? cashier[avail] : T;
cashier[avail] += D;
}
cout << clientsOverTime << endl;
inFile.close();
delete [] cashier;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment