#include <iostream>
#include <math.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string.h>
using namespace std;
class Task {
public:
char name[20];
int ready,end,running,waiting;
int arrival;
int priority;
float age;
int tickets;
int servingTimeTotal,servingTime;
public:
Task();
Task(string id,int arrival,int priority,int age,int tickets);
void print();
};
class TaskList {
public:
vector<Task> allTalks;
public:
void load(string filepath);
vector<Task> getTaskBefore(int time);
vector<Task> getTaskGreater(int time);
vector<Task> getByArrivalLess(int time);
void print();
};
public:
vector<Task> allTalks;
public:
void load(string filepath);
vector<Task> getTaskBefore(int time);
vector<Task> getTaskGreater(int time);
vector<Task> getByArrivalLess(int time);
void print();
};
class Queue1 {
vector<Task> queue1;
Task demoted;
public:
bool status;
public:
void enQueue(Task c);
void round(int time);
int getSize();
Task getDemoted();
};
TaskList taskList;
vector<Task> queue1;
Task demoted;
public:
bool status;
public:
void enQueue(Task c);
void round(int time);
int getSize();
Task getDemoted();
};
TaskList taskList;
class Queue2 {
vector<Task> queue2;
vector<Task> promoted;
public:
bool status;
public:
void append(Task c);
void enQueue(Task c);
void round(int time);
int getLength();
void aging();
void interupt();
vector<Task> getpromoted();
};
vector<Task> queue2;
vector<Task> promoted;
public:
bool status;
public:
void append(Task c);
void enQueue(Task c);
void round(int time);
int getLength();
void aging();
void interupt();
vector<Task> getpromoted();
};
Queue2 queue1;
Queue1 queue2;
Task::Task() {
end = ready = -1;
running = waiting = 0;
servingTime = servingTimeTotal = 0;
}
Task::Task(string id,int arrival,int priority,int age,int tickets) {
// TODO Auto-generated constructor stub
strcpy(this->name,id.c_str());
this->arrival = arrival;
this->priority = priority;
this->age = age;
this->tickets = tickets;
// TODO Auto-generated constructor stub
strcpy(this->name,id.c_str());
this->arrival = arrival;
this->priority = priority;
this->age = age;
this->tickets = tickets;
end = ready = -1;
running = waiting = 0;
servingTime = servingTimeTotal = 0;
}
//name arrival end ready running waiting age priority
void Task::print() {
cout << name << "\t" << arrival << "\t" << end << "\t" << ready << "\t" << running << "\t"
<< waiting << "\t" << age << "\t" << priority << endl;
}
void Task::print() {
cout << name << "\t" << arrival << "\t" << end << "\t" << ready << "\t" << running << "\t"
<< waiting << "\t" << age << "\t" << priority << endl;
}
void Queue1::enQueue(Task c) {
if(queue1.size() == 0){
queue1.push_back(c);
return;
}
if(queue1.size() == 0){
queue1.push_back(c);
return;
}
//find index to put in
int index = -1;
for(int i=0;i<queue1.size();i++){
if(queue1[i].priority < c.priority){
index = i;
break;
}
}
int index = -1;
for(int i=0;i<queue1.size();i++){
if(queue1[i].priority < c.priority){
index = i;
break;
}
}
if(index == -1){
queue1.push_back(c);
}else{
if(index == 0){
if(queue1[0].servingTime > 0){
queue1.insert(queue1.begin()+1,c);
}else {
queue1.insert(queue1.begin(),c);
}
}else{
queue1.insert(queue1.begin()+index,c);
}
}
}
queue1.push_back(c);
}else{
if(index == 0){
if(queue1[0].servingTime > 0){
queue1.insert(queue1.begin()+1,c);
}else {
queue1.insert(queue1.begin(),c);
}
}else{
queue1.insert(queue1.begin()+index,c);
}
}
}
void Queue1::round(int time) {
status = false;
status = false;
if(queue1.size()>0){
//serve first
if(queue1[0].running == 0){
queue1[0].ready = time;
}
queue1[0].running++;
queue1[0].servingTime++;
queue1[0].servingTimeTotal++;
queue1[0].age = 0;
//serve first
if(queue1[0].running == 0){
queue1[0].ready = time;
}
queue1[0].running++;
queue1[0].servingTime++;
queue1[0].servingTimeTotal++;
queue1[0].age = 0;
//others waiting
for(int i=1;i<queue1.size();i++){
if(queue1[i].ready != -1)queue1[i].waiting++;
}
for(int i=1;i<queue1.size();i++){
if(queue1[i].ready != -1)queue1[i].waiting++;
}
//check status of first
if(queue1[0].running >= queue1[0].tickets){
queue1[0].servingTimeTotal = 0;
queue1[0].servingTime = 0;
queue1[0].end = time+1;
if(queue1[0].running >= queue1[0].tickets){
queue1[0].servingTimeTotal = 0;
queue1[0].servingTime = 0;
queue1[0].end = time+1;
//output
cout << queue1[0].name << "\t"
<< queue1[0].arrival << "\t"
<< queue1[0].end << "\t"
<< queue1[0].ready << "\t"
<< queue1[0].running << "\t"
<< queue1[0].waiting << "\t"
<< endl;
cout << queue1[0].name << "\t"
<< queue1[0].arrival << "\t"
<< queue1[0].end << "\t"
<< queue1[0].ready << "\t"
<< queue1[0].running << "\t"
<< queue1[0].waiting << "\t"
<< endl;
queue1.erase(queue1.begin());
}else{
if(queue1[0].servingTime >= 5){
queue1[0].servingTime = 0;
if(queue1[0].servingTimeTotal >= 25){
queue1[0].servingTimeTotal = 0;
queue1[0].priority–;
if(queue1[0].priority <= 2){
demoted = queue1[0];
status = true;
}else{
}else{
if(queue1[0].servingTime >= 5){
queue1[0].servingTime = 0;
if(queue1[0].servingTimeTotal >= 25){
queue1[0].servingTimeTotal = 0;
queue1[0].priority–;
if(queue1[0].priority <= 2){
demoted = queue1[0];
status = true;
}else{
//new come
vector<Task> newcomes = taskList.getTaskGreater(time+1);
for(int i=0;i<newcomes.size();i++){
if(newcomes[i].priority > 2){
enQueue(newcomes[i]);
}
}
enQueue(queue1[0]);
}
}else{
//new come
vector<Task> newcomes = taskList.getTaskGreater(time+1);
for(int i=0;i<newcomes.size();i++){
if(newcomes[i].priority > 2){
enQueue(newcomes[i]);
}
}
enQueue(queue1[0]);
}
queue1.erase(queue1.begin());
}
}
}
}
vector<Task> newcomes = taskList.getTaskGreater(time+1);
for(int i=0;i<newcomes.size();i++){
if(newcomes[i].priority > 2){
enQueue(newcomes[i]);
}
}
enQueue(queue1[0]);
}
}else{
//new come
vector<Task> newcomes = taskList.getTaskGreater(time+1);
for(int i=0;i<newcomes.size();i++){
if(newcomes[i].priority > 2){
enQueue(newcomes[i]);
}
}
enQueue(queue1[0]);
}
queue1.erase(queue1.begin());
}
}
}
}
int Queue1::getSize() {
return queue1.size();
}
return queue1.size();
}
Task Queue1::getDemoted() {
demoted.servingTimeTotal = demoted.servingTime = 0;
return demoted;
}
demoted.servingTimeTotal = demoted.servingTime = 0;
return demoted;
}
bool sortByArrival( const Task &v1, const Task &v2) {
return v1.arrival < v2.arrival;
}
return v1.arrival < v2.arrival;
}
void TaskList::load(string filepath) {
FILE *fp = fopen(filepath.c_str(),"r");
if(fp != NULL){
Task task;
fscanf(fp,"%s%d%d%f%d\n",task.name,&task.arrival,&task.priority,&task.age,&task.tickets);
while(!feof(fp)){
allTalks.push_back(task);
fscanf(fp,"%s%d%d%f%d\n",task.name,&task.arrival,&task.priority,&task.age,&task.tickets);
}
allTalks.push_back(task);
fclose(fp);
}
//sort
std::sort(allTalks.begin(), allTalks.end(), sortByArrival);
}
FILE *fp = fopen(filepath.c_str(),"r");
if(fp != NULL){
Task task;
fscanf(fp,"%s%d%d%f%d\n",task.name,&task.arrival,&task.priority,&task.age,&task.tickets);
while(!feof(fp)){
allTalks.push_back(task);
fscanf(fp,"%s%d%d%f%d\n",task.name,&task.arrival,&task.priority,&task.age,&task.tickets);
}
allTalks.push_back(task);
fclose(fp);
}
//sort
std::sort(allTalks.begin(), allTalks.end(), sortByArrival);
}
vector<Task> TaskList::getTaskBefore(int time) {
vector<Task> ret;
for(vector<Task>::iterator iter=allTalks.begin();iter!=allTalks.end();){
if(iter->arrival <= time){
ret.push_back(*iter);
allTalks.erase(iter);
}else{
iter++;
}
}
return ret;
}
vector<Task> ret;
for(vector<Task>::iterator iter=allTalks.begin();iter!=allTalks.end();){
if(iter->arrival <= time){
ret.push_back(*iter);
allTalks.erase(iter);
}else{
iter++;
}
}
return ret;
}
vector<Task> TaskList::getTaskGreater(int time) {
vector<Task> ret;
for(vector<Task>::iterator iter=allTalks.begin();iter!=allTalks.end();){
if(iter->arrival <= time && iter->priority > 2){
ret.push_back(*iter);
allTalks.erase(iter);
}else{
iter++;
}
}
return ret;
}
vector<Task> ret;
for(vector<Task>::iterator iter=allTalks.begin();iter!=allTalks.end();){
if(iter->arrival <= time && iter->priority > 2){
ret.push_back(*iter);
allTalks.erase(iter);
}else{
iter++;
}
}
return ret;
}
vector<Task> TaskList::getByArrivalLess(int time) {
vector<Task> ret;
for(vector<Task>::iterator iter=allTalks.begin();iter!=allTalks.end();){
if(iter->arrival <= time && iter->priority <= 2){
ret.push_back(*iter);
allTalks.erase(iter);
}else{
iter++;
}
}
return ret;
}
vector<Task> ret;
for(vector<Task>::iterator iter=allTalks.begin();iter!=allTalks.end();){
if(iter->arrival <= time && iter->priority <= 2){
ret.push_back(*iter);
allTalks.erase(iter);
}else{
iter++;
}
}
return ret;
}
void TaskList::print() {
for(int i=0;i<allTalks.size();i++){
allTalks[i].print();
}
cout << endl;
}
for(int i=0;i<allTalks.size();i++){
allTalks[i].print();
}
cout << endl;
}
void Queue2::append(Task c){
c.servingTimeTotal = c.servingTime = 0;
queue2.push_back(c);
}
c.servingTimeTotal = c.servingTime = 0;
queue2.push_back(c);
}
void Queue2::enQueue(Task c) {
c.servingTimeTotal = c.servingTime = 0;
if(queue2.size() == 0){
queue2.push_back(c);
return;
}
c.servingTimeTotal = c.servingTime = 0;
if(queue2.size() == 0){
queue2.push_back(c);
return;
}
//find index to put in
int index = -1;
for(int i=0;i<queue2.size();i++){
if(queue2[i].priority >= c.priority){
int index = -1;
for(int i=0;i<queue2.size();i++){
if(queue2[i].priority >= c.priority){
}else{
index = i;
break;
}
}
index = i;
break;
}
}
if(index == -1){//no index found, push back
queue2.push_back(c);
}else{
if(index == 0){
if(queue2[0].servingTime > 0){
queue2[0].servingTime = 0;
queue2[0].age–;
}
queue2.insert(queue2.begin(),c);
}else{
queue2.insert(queue2.begin()+index,c);
}
}
}
queue2.push_back(c);
}else{
if(index == 0){
if(queue2[0].servingTime > 0){
queue2[0].servingTime = 0;
queue2[0].age–;
}
queue2.insert(queue2.begin(),c);
}else{
queue2.insert(queue2.begin()+index,c);
}
}
}
void Queue2::round(int time) {
status = false;
status = false;
if(queue2.size()>0){
//serve first
if(queue2[0].running == 0){
queue2[0].ready = time;
}
queue2[0].running++;
queue2[0].servingTime++;
queue2[0].age = 0;
//serve first
if(queue2[0].running == 0){
queue2[0].ready = time;
}
queue2[0].running++;
queue2[0].servingTime++;
queue2[0].age = 0;
//others waiting
for(int i=1;i<queue2.size();i++){
if(queue2[i].ready != -1)queue2[i].waiting++;
queue2[i].age += 0.05;
if(queue2[i].age >= 7.99){
queue2[i].age = 0;
queue2[i].priority++;
}
}
for(int i=1;i<queue2.size();i++){
if(queue2[i].ready != -1)queue2[i].waiting++;
queue2[i].age += 0.05;
if(queue2[i].age >= 7.99){
queue2[i].age = 0;
queue2[i].priority++;
}
}
//aging
promoted.clear();
for(vector<Task>::iterator iter=queue2.begin();iter!=queue2.end();){
if( (*iter).priority >= 3 ){
promoted.push_back(*iter);
queue2.erase(iter);
}else{
iter++;
}
}
if(promoted.size() > 0){
status = true;
}
promoted.clear();
for(vector<Task>::iterator iter=queue2.begin();iter!=queue2.end();){
if( (*iter).priority >= 3 ){
promoted.push_back(*iter);
queue2.erase(iter);
}else{
iter++;
}
}
if(promoted.size() > 0){
status = true;
}
//check status of first
if(queue2[0].running >= queue2[0].tickets){
queue2[0].servingTime = 0;
queue2[0].end = time+1;
if(queue2[0].running >= queue2[0].tickets){
queue2[0].servingTime = 0;
queue2[0].end = time+1;
cout << queue2[0].name << "\t"
<< queue2[0].arrival << "\t"
<< queue2[0].end << "\t"
<< queue2[0].ready << "\t"
<< queue2[0].running << "\t"
<< queue2[0].waiting << "\t"
<< endl;
<< queue2[0].arrival << "\t"
<< queue2[0].end << "\t"
<< queue2[0].ready << "\t"
<< queue2[0].running << "\t"
<< queue2[0].waiting << "\t"
<< endl;
queue2.erase(queue2.begin());
}else{
if(queue2[0].servingTime >= 20){
queue2[0].servingTime = 0;
//new come
vector<Task> newcomes = taskList.getByArrivalLess(time+1);
for(int i=0;i<newcomes.size();i++){
if(newcomes[i].priority <= 2){
append(newcomes[i]);
}
}
append(queue2[0]);
queue2.erase(queue2.begin());
}
}
}
}
}else{
if(queue2[0].servingTime >= 20){
queue2[0].servingTime = 0;
//new come
vector<Task> newcomes = taskList.getByArrivalLess(time+1);
for(int i=0;i<newcomes.size();i++){
if(newcomes[i].priority <= 2){
append(newcomes[i]);
}
}
append(queue2[0]);
queue2.erase(queue2.begin());
}
}
}
}
int Queue2::getLength() {
return queue2.size();
}
return queue2.size();
}
void Queue2::interupt(){
if(queue2.size() > 0){
for(int i=1;i<queue2.size();i++){
queue2[i].age = (int)(queue2[i].age + 1);
}
queue2[0].age = 0;
append(queue2[0]);
queue2.erase(queue2.begin());
}
}
void Queue2::aging() {
//others waiting
status = false;
for(int i=0;i<queue2.size();i++){
if(queue2[i].ready != -1)queue2[i].waiting++;
queue2[i].age += 0.2;
float th = 7.999;
//cout << customers[i].waiting << " " << customers[i].age << " " << (customers[i].age>=th?"t":"f") << endl;
if(queue2[i].age >= th){
queue2[i].age = 0;
queue2[i].priority++;
}
}
//others waiting
status = false;
for(int i=0;i<queue2.size();i++){
if(queue2[i].ready != -1)queue2[i].waiting++;
queue2[i].age += 0.2;
float th = 7.999;
//cout << customers[i].waiting << " " << customers[i].age << " " << (customers[i].age>=th?"t":"f") << endl;
if(queue2[i].age >= th){
queue2[i].age = 0;
queue2[i].priority++;
}
}
//aging
promoted.clear();
for(vector<Task>::iterator iter=queue2.begin();iter!=queue2.end();){
if( (*iter).priority >= 3 ){
promoted.push_back(*iter);
queue2.erase(iter);
}else{
iter++;
}
}
if(promoted.size() > 0){
status = true;
}
}
promoted.clear();
for(vector<Task>::iterator iter=queue2.begin();iter!=queue2.end();){
if( (*iter).priority >= 3 ){
promoted.push_back(*iter);
queue2.erase(iter);
}else{
iter++;
}
}
if(promoted.size() > 0){
status = true;
}
}
vector<Task> Queue2::getpromoted() {
for(int i=0;i<promoted.size();i++){
promoted[i].servingTimeTotal = promoted[i].servingTime = 0;
}
return promoted;
}
for(int i=0;i<promoted.size();i++){
promoted[i].servingTimeTotal = promoted[i].servingTime = 0;
}
return promoted;
}
int main(int argc, char**argv) {
taskList.load(argv[1]);
taskList.load(argv[1]);
cout << "name" << "\t"
<< "arrival" << "\t"
<< "end" << "\t"
<< "ready" << "\t"
<< "running" << "\t"
<< "waiting" << "\t"
<< endl;
<< "arrival" << "\t"
<< "end" << "\t"
<< "ready" << "\t"
<< "running" << "\t"
<< "waiting" << "\t"
<< endl;
for(int time=0;true;time++){
vector<Task> list = taskList.getTaskBefore(time);
for(int i=0;i<list.size();i++){
if(list[i].priority > 2){
queue2.enQueue(list[i]);
if(queue2.getSize()==1){
queue1.interupt();
}
}else{
queue1.append(list[i]);
}
}
vector<Task> list = taskList.getTaskBefore(time);
for(int i=0;i<list.size();i++){
if(list[i].priority > 2){
queue2.enQueue(list[i]);
if(queue2.getSize()==1){
queue1.interupt();
}
}else{
queue1.append(list[i]);
}
}
//run
if(queue2.getSize() > 0){
queue2.round(time);
queue1.aging();
bool status1 = queue2.status;
bool status2 = queue1.status;
if(queue2.getSize() > 0){
queue2.round(time);
queue1.aging();
bool status1 = queue2.status;
bool status2 = queue1.status;
if(status1){
//new come
vector<Task> newcomes = taskList.getByArrivalLess(time+1);
for(int i=0;i<newcomes.size();i++){
if(newcomes[i].priority <= 2){
queue1.append(newcomes[i]);
}
}
//new come
vector<Task> newcomes = taskList.getByArrivalLess(time+1);
for(int i=0;i<newcomes.size();i++){
if(newcomes[i].priority <= 2){
queue1.append(newcomes[i]);
}
}
//demote
Task demoted = queue2.getDemoted();
queue1.append(demoted);
}
if(status2){
vector<Task> promoted = queue1.getpromoted();
for(int i=0;i<promoted.size();i++){
queue2.enQueue(promoted[i]);
}
}
}else{
if(queue1.getLength() > 0){
queue1.round(time);
bool status2 = queue1.status;
if(status2){
vector<Task> promoted = queue1.getpromoted();
for(int i=0;i<promoted.size();i++){
queue2.enQueue(promoted[i]);
}
}
}else{
break;
}
}
}
Task demoted = queue2.getDemoted();
queue1.append(demoted);
}
if(status2){
vector<Task> promoted = queue1.getpromoted();
for(int i=0;i<promoted.size();i++){
queue2.enQueue(promoted[i]);
}
}
}else{
if(queue1.getLength() > 0){
queue1.round(time);
bool status2 = queue1.status;
if(status2){
vector<Task> promoted = queue1.getpromoted();
for(int i=0;i<promoted.size();i++){
queue2.enQueue(promoted[i]);
}
}
}else{
break;
}
}
}
return 0;
}
}