-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path20170902.cpp
More file actions
88 lines (82 loc) · 1.32 KB
/
20170902.cpp
File metadata and controls
88 lines (82 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int n, k, w, s, c, key[1001];
struct Teacher//定义教师结构体
{
int key;//钥匙编号
int time;//使用钥匙时间
int flag;//设置标识符
};
bool cmp(const Teacher& t1, const Teacher& t2)//自定义比较函数
{
if (t1.time != t2.time)//使用时间少的先还
{
return t1.time < t2.time;
}
else if (t1.flag != t2.flag)//先归还后取出
{
return t1.flag > t2.flag;
}
else//钥匙编号从小到大归还
{
return t1.key < t2.key;
}
}
int main()
{
std::ios::sync_with_stdio(false);
vector<Teacher> v;//定义结构体向量
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
key[i] = i;
}
for (int i = 0; i < k; i++)
{
cin >> w >> s >> c;
Teacher t;
t.key = w;
t.time = s;
t.flag = 0;//设置取出标识
v.push_back(t);
t.key = w;
t.time = s + c;
t.flag = 1;//设置归还标识
v.push_back(t);
}
sort(v.begin(), v.end(), cmp);//自定义排序
for (int i = 0; i < v.size(); i++)
{
Teacher t = v[i];
if (!t.flag)//取出钥匙
{
for (int j = 1; j <= n; j++)
{
if (key[j] == t.key)
{
key[j] = 0;
break;
}
}
}
else//归还钥匙
{
for (int j = 1; j <= n; j++)
{
if (key[j] == 0)//找到空挂钩
{
key[j] = t.key;
break;
}
}
}
}
for (int i = 1; i <= n; i++)
{
cout << key[i] << " ";
}
cout << endl;
return 0;
}