您的位置:首页 > 编程语言 > C语言/C++

CCF-老师拿钥匙20170902

2017-11-14 14:17 162 查看
#include <bits/stdc++.h>

using namespace std;
/***********************class**********************************/

class Action {

    public:

        int room;  //钥匙号

        int time;  //归还事件发生的时间

        int type; // 0:还钥匙, 1:借钥匙

        //构造函数(初始化列表)进行类的初始化

        Action(int room_, int time_, int type_) : room(room_), time(time_), type(type_) {}
/********************重载operator<函数 *******************************/      

        bool operator<(const Action &other) const {

            if(time<other.time) return true;

            else if(time==other.time && type<other.type) return true;

            else if(time==other.time && type==other.type && room<other.room) return true;

            return false;

         }

};
/*

********************重写operator<函数 *******************************

小于号时升序排序

int cmp(Action action1,Action action2)

{

    if(action1.time < action2.time){

        return 1;

    }else if(action1.time == action2.time

            && action1.type < action2.type){

        return 1;

    }else if(action1.time == action2.time

            && action1.type == action2.type

            && action1.room < action2.room){

            return 1;  

    }

    return 0;

}
*/
int main() {

    int N, K;

    cin>>N>>K;

    //创建Action类型的Vector数组

    vector<Action> actions;

    //钥匙的初始化排序

    vector<int> result(N+1);

    for(int n=1; n<=N; n++) result
= n;
    for(int k=0; k<K; k++) {

        int room, begin, length;

        cin>>room>>begin>>length;

        actions.push_back(Action(room, begin, 1));//借钥匙

        actions.push_back(Action(room, begin+length, 0));

    }
/*************sort利用operator<重写函数进行排序 *************************/

    sort(actions.begin(), actions.end());
/***********************取放操作**********************************/   

    for(int i=0; i<actions.size(); i++) {

        Action &act = actions[i];

        if(act.type == 0) { // put放

            for(int n=1; n<=N; n++) {

                if(result
== -1) {

                    result
= act.room;

                    break;

                }

            }

        }

        else { // get 取

            for(int n=1; n<=N; n++) {

                if(result
== act.room) {

                    result
= -1;

                    break;

                }

            }

        }

    }
/***********************结果输出**********************************/   

    for(int n=1; n<=N; n++) {

        cout<<result
<<" ";

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++