Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
13 kB
7
Indexable
Never
#include<bits/stdc++.h>
using namespace std;

class Platform{
    public:
    vector<int> platform_wise_free_time;
    Platform(int number_of_platforms){
        platform_wise_free_time.resize(number_of_platforms);
    }

    vector<int> find_platform_details(int eta, int etd, bool hasTrainPassed, int train_platform, int clearance_time){
        int train_platform_assigned = -1;
        if (train_platform != -1){
            train_platform_assigned = train_platform;
            if (eta - clearance_time > platform_wise_free_time[train_platform]){
                platform_wise_free_time[train_platform] = etd+clearance_time;
            }
            else{
                int difference_in_time = etd - eta;
                eta=platform_wise_free_time[train_platform] + clearance_time;
                etd=eta+difference_in_time;
                platform_wise_free_time[train_platform] = etd+clearance_time;

            }
        }
        else{
            for(int plt=0;plt<platform_wise_free_time.size();plt++){
                if (eta - clearance_time > platform_wise_free_time[plt]{
                    platform_wise_free_time[train_platform] = etd+clearance_time;
                    train_platform_assigned = plt;
                    break;
                }
            }
        }
        return {train_platform_assigned, eta, etd};
    }
};

class Train{
public:
    int tid;
    vector<int> eta;
    vector<int> etd;
    int platform;
    bool hasTrainPassed = false;
    int platformPassed = -1;
};

class NormalTrain: public Train{
public:
    static int clearance_time = 5;
};

class NonStopTrain: public Train{
public:
    static int clearance_time = 1;
};

class ExpressTrain: public Train{
public:
    static int clearance_time = 3;
};

void printTrainDetails(map<int, Train> train_id_wise_train){
    vector <vector<int>> train_details_day_1;
    vector <vector<int>> train_details_day_2;
    for (auto train: train_id_wise_train){
        int tid = train.first;
        vector<Train> trains = train.second;
        for(auto tr: trains){
            int eta = tr.eta;
            int etd = tr.etd;
            int platform = tr.platform;
            if (eta> 1440){
                train_details_day_2.push_back({eta-1440, etd, platform, tid});
            }
            else{
                train_details_day_1.push_back({eta, etd, platform, tid});
            }
        }
    }
    sort(train_details_day_1.begin(), train_details_day_1.end());
    sort(train_details_day_2.begin(), train_details_day_2.end());
    cout<<"Day 1"<<endl;
    for(int i=0;i<train_details_day_1.size();i++){
        cout<<train_details_day_1[i][0]<<" "<<train_details_day_1[i][1]<<" "<<train_details_day_1[i][2]<<" "<<train_details_day_1[i][3]<<endl;
    }
    cout<<endl;
    cout<<"Day 1"<<endl;
    for(int i=0;i<train_details_day_2.size();i++){
        cout<<train_details_day_2[i][0]<<" "<<train_details_day_2[i][1]<<" "<<train_details_day_2[i][2]<<" "<<train_details_day_2[i][3]<<endl;
    }
}

int main(){
    int number_of_platforms;
    cin>>number_of_platforms;
    Platform platform(number_of_platforms);
    int type = 1;
    while(type != 3){


        cin>>type;
        int tid, string type, string eta, string etd;

        map<int, vector<Train>> train_id_wise_train;

        if (type == 1){
            cin>>tid>>type>>eta>>etd;
            int hours_eta = stoi(eta[0:2]);
            int mins_eta = stoi(eta[3:]);
            int eta_mins = 24*hours_eta + mins_eta;

            int hours_etd = stoi(etd[0:2]);
            int mins_etd = stoi(etd[3:]);
            int etd_mins = 24*hours_etd + mins_etd;


            if (train_id_wise_train.find(tid) == train_id_wise_train.end()){
                bool has_train_passed = false;
                int clearance_time = 0;
                if (type == "NORMAL"){
                    clearance_time = NormalTrain.clearance_time;
                }
                if (type == "EXPRESS"){
                    clearance_time = ExpressTrain.clearance_time;
                }
                if (type == "NONSTOP"){
                    clearance_time = NonStopTrain.clearance_time;
                }
                int platform_train, train_actual_eta, train_actual_etd;
                vector<int> train_platform_details = platform.find_platform_details(eta, etd, has_train_passed, -1, clearance_time);
                platform_train = train_platform_details[0];
                train_actual_eta = train_platform_details[1];
                train_actual_etd = train_platform_details[2];
                Train train;
                if (type == "NORMAL"){
                    clearance_time = NormalTrain.clearance_time;
                    train.eta = train_actual_eta;
                    train.etd = train_actual_etd;
                    train.platform = platform_train;
                }
                if (type == "EXPRESS"){
                    clearance_time = ExpressTrain.clearance_time;
                    train.eta = train_actual_eta;
                    train.etd = train_actual_etd;
                    train.platform = platform_train;
                    train.hasTrainPassed = true;
                    train.platformPassed = platform_train;
                }
                if (type == "NONSTOP"){
                    clearance_time = NonStopTrain.clearance_time;
                    train.eta = train_actual_eta;
                    train.etd = train_actual_etd;
                    train.platform = platform_train;
                }
                train_id_wise_train[train.tid] = {train};

            }
            else{
                Train train_details = train_id_wise_train[tid][train_id_wise_train[tid].size()-1];
                int platform_through_which_it_passed = -1;
                bool has_train_passed = false;
                int clearance_time = 0;
                if (type == "NORMAL"){
                    clearance_time = NormalTrain.clearance_time;
                }
                if (type == "EXPRESS"){
                    clearance_time = ExpressTrain.clearance_time;
                    has_train_passed = true;
                    platform_through_which_it_passed = train_details.platformPassed;
                }
                if (type == "NONSTOP"){
                    clearance_time = NonStopTrain.clearance_time;
                }
                int platform_train, train_actual_eta, train_actual_etd;
                vector<int> train_platform_details = platform.find_platform_details(eta, etd, has_train_passed, platform_through_which_it_passed, clearance_time);
                platform_train = train_platform_details[0];
                train_actual_eta = train_platform_details[1];
                train_actual_etd = train_platform_details[2];
                Train train;
                if (type == "NORMAL"){
                    clearance_time = NormalTrain.clearance_time;
                    train.eta = train_actual_eta;
                    train.etd = train_actual_etd;
                    train.platform = platform_train;
                }
                if (type == "EXPRESS"){
                    clearance_time = ExpressTrain.clearance_time;
                    train.eta = train_actual_eta;
                    train.etd = train_actual_etd;
                    train.platform = platform_train;
                    train.hasTrainPassed = true;
                    train.platformPassed = platform_train;
                }
                if (type == "NONSTOP"){
                    clearance_time = NonStopTrain.clearance_time;
                    train.eta = train_actual_eta;
                    train.etd = train_actual_etd;
                    train.platform = platform_train;
                }
                train_id_wise_train[train.tid].push_back(train);
            }
            printTrainDetails(train_id_wise_train);
        }
        else if (type == 2){
            cin>>tid>>type>>eta>>etd;
            int hours_eta = stoi(eta[0:2]);
            int mins_eta = stoi(eta[3:]);
            int eta_mins = 24*hours_eta + mins_eta;

            int hours_etd = stoi(etd[0:2]) + 24;
            int mins_etd = stoi(etd[3:]);
            int etd_mins = 24*hours_etd + mins_etd;


            if (train_id_wise_train.find(tid) == train_id_wise_train.end()){
                bool has_train_passed = false;
                int clearance_time = 0;
                if (type == "NORMAL"){
                    clearance_time = NormalTrain.clearance_time;
                }
                if (type == "EXPRESS"){
                    clearance_time = ExpressTrain.clearance_time;
                }
                if (type == "NONSTOP"){
                    clearance_time = NonStopTrain.clearance_time;
                }
                int platform_train, train_actual_eta, train_actual_etd;
                vector<int> train_platform_details = platform.find_platform_details(eta, etd, has_train_passed, -1, clearance_time);
                platform_train = train_platform_details[0];
                train_actual_eta = train_platform_details[1];
                train_actual_etd = train_platform_details[2];
                Train train;
                if (type == "NORMAL"){
                    clearance_time = NormalTrain.clearance_time;
                    train.eta = train_actual_eta;
                    train.etd = train_actual_etd;
                    train.platform = platform_train;
                }
                if (type == "EXPRESS"){
                    clearance_time = ExpressTrain.clearance_time;
                    train.eta = train_actual_eta;
                    train.etd = train_actual_etd;
                    train.platform = platform_train;
                    train.hasTrainPassed = true;
                    train.platformPassed = platform_train;
                }
                if (type == "NONSTOP"){
                    clearance_time = NonStopTrain.clearance_time;
                    train.eta = train_actual_eta;
                    train.etd = train_actual_etd;
                    train.platform = platform_train;
                }
                train_id_wise_train[train.tid] = {train};

            }
            else{
                Train train_details = train_id_wise_train[tid][train_id_wise_train[tid].size()-1];
                int platform_through_which_it_passed = -1;
                bool has_train_passed = false;
                int clearance_time = 0;
                if (type == "NORMAL"){
                    clearance_time = NormalTrain.clearance_time;
                }
                if (type == "EXPRESS"){
                    clearance_time = ExpressTrain.clearance_time;
                    has_train_passed = true;
                    platform_through_which_it_passed = train_details.platformPassed;
                }
                if (type == "NONSTOP"){
                    clearance_time = NonStopTrain.clearance_time;
                }
                int platform_train, train_actual_eta, train_actual_etd;
                vector<int> train_platform_details = platform.find_platform_details(eta, etd, has_train_passed, platform_through_which_it_passed, clearance_time);
                platform_train = train_platform_details[0];
                train_actual_eta = train_platform_details[1];
                train_actual_etd = train_platform_details[2];
                Train train;
                if (type == "NORMAL"){
                    clearance_time = NormalTrain.clearance_time;
                    train.eta = train_actual_eta;
                    train.etd = train_actual_etd;
                    train.platform = platform_train;
                }
                if (type == "EXPRESS"){
                    clearance_time = ExpressTrain.clearance_time;
                    train.eta = train_actual_eta;
                    train.etd = train_actual_etd;
                    train.platform = platform_train;
                    train.hasTrainPassed = true;
                    train.platformPassed = platform_train;
                }
                if (type == "NONSTOP"){
                    clearance_time = NonStopTrain.clearance_time;
                    train.eta = train_actual_eta;
                    train.etd = train_actual_etd;
                    train.platform = platform_train;
                }
                train_id_wise_train[train.tid].push_back(train);
            }
            printTrainDetails(train_id_wise_train);
        }
        else{
            printTrainDetails(train_id_wise_train);
            break;
        }
    }
    return 0;
}
Leave a Comment