问题描述
思路
完整代码
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX = 1010;
int N, K;
struct node
{
int w; //钥匙编号
int s; //上课开始时间
int c; //上课时长
int e; //上课结束时间
}Teacher[MAX];
int cmd(const node &a,const node &b)
{
return a.w<b.w;
}
int main()
{
cin >> N >> K;
int Start = -1, End = -1; //记录最晚借钥匙和最晚还钥匙时间
for( int i = 1; i <= K; i++)
{
cin >> Teacher[i].w >> Teacher[i].s >> Teacher[i].c;
Teacher[i].e = Teacher[i].s + Teacher[i].c;
End = max( End, Teacher[i].e);
Start = max( Start, Teacher[i].s);
}
int key[N+1], pos[N+1];
for( int i = 1; i <= N; i++)
{
key[i] = i; //第i个位置上放几号钥匙,若为0则代表该位置没有钥匙
pos[i] = i; //第i号钥匙在哪个位置
}
for( int i = 1; i <= End; i++)
{
sort(Teacher+1, Teacher+K+1, cmd); //按照钥匙编号对Teacher进行排序
//还钥匙
for( int j = 1; j <= K; j++)
{
if( Teacher[j].e == i)
{//该老师还钥匙
for( int m = 1; m <= N; m++)
{//找没有挂钥匙的空位
if( key[m] == 0 )
{
key[m] = Teacher[j].w;
pos[Teacher[j].w] = m;
break;
}
}
}
}
//取钥匙
if( i <= Start)
{
for( int j = 1; j <= K; j++)
{
if( Teacher[j].s == i)
{
key[pos[Teacher[j].w]] = 0; //该位置上没有钥匙
}
}
}
}
for( int i = 1; i <= N; i++)
{
cout << key[i] << ' ';
}
return 0;
}