#include
#include
#include
using namespace std;
struct Action {
int room;
int time;
int type; // 0:put, 1:get
Action(int room_, int time_, int type_) : room(room_), time(time_), type(type_) {}//?
bool operator<(const Action &other) const {//?
if(time<other.time) return true;//other.time?
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;
}
};
int main() {
int N, K;
scanf("%d%d", &N, &K);
vector actions;
vector states(N+1);
for(int n=1; n<=N; n++) states[n] = n;
for(int k=0; k<K; k++) {
int room, begin, length;
scanf("%d%d%d", &room, &begin, &length);
actions.push_back(Action(room, begin, 1));
actions.push_back(Action(room, begin+length, 0));
}
sort(actions.begin(), actions.end());
for(int i=0; i<actions.size(); i++) {
Action &act = actions[i];//&act什么意思?
if(act.type == 0) { // put
for(int n=1; n<=N; n++) {
if(states[n] == -1) {
states[n] = act.room;
break;
}
}
}
else { // get
for(int n=1; n<=N; n++) {
if(states[n] == act.room) {
states[n] = -1;
break;
}
}
}
}
for(int n=1; n<=N; n++) {
printf("%d ", states[n]);
}
}
Action(int room_, int time_, int type_) : room(room_), time(time_), type(type_) {}//?
这句是构造函数的初始化的一种方法,是把room_的值赋值给room,把time_赋值给time,type_赋值给type
bool operator<(const Action &other) const {//?
if(time<other.time) return true;//other.time?
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;
这个是重载<符号的函数,const表示函数内容不能被修改,这保护了你函数的安全
如果time<other.time 是判断两个变量的大小。other是你传进来的参数,也就是另外一个类,other.time是类里面的变量time,其他同理
这个意思是如果time小于other.time,就返回true,如果等于,再判断 type,如果 type<other.type那就返回true,
相等就再判断room和other.room,如果room<other.room也还是返回TRUE,
所有都不满足,最后返回return false;
全是手打,很辛苦,求采纳,谢谢,我十分需要你的采纳
非常感谢楼上的回答,谢谢