问题描述 N个挂钩上有N个钥匙。钥匙没有固定的悬挂位置,但钥匙上有标号。
每次取钥匙的时候,而不会移动其他钥匙。
每次还钥匙的时候,挂在最左边的空挂钩 上。
同一时刻先还再取 ,且按编号从小到大 的顺序还。
初始时,钥匙按编号从小到大 挂着。
有K 位老师取。给出钥匙标号、取出时间和借出时长,请问最终钥匙盒里面钥匙的顺序是怎样的?
输入格式 输入的第一行包含两个整数N , K 。 接下来K 行,每行三个整数w , s , c ,分别表示一位老师要使用的钥匙编号、取出时间和借出的时长。可能有多位老师使用同一把钥匙,但是老师使用钥匙的时间不会重叠。
输出格式 输出一行,包含N 个整数,相邻整数间用一个空格分隔,依次表示每个挂钩上挂的钥匙编号。
样例输入 5 2 4 3 3 2 2 7
样例输出 1 4 3 2 5
样例说明 第一位老师从时刻3开始使用4号教室的钥匙,使用3单位时间,所以在时刻6还钥匙。第二位老师从时刻2开始使用钥匙,使用7单位时间,所以在时刻9还钥匙。 每个关键时刻后的钥匙状态如下(X表示空): 时刻2后为1X345; 时刻3后为1X3X5; 时刻6后为143X5; 时刻9后为14325。
样例输入 5 7 1 1 14 3 3 12 1 15 12 2 7 20 3 18 12 4 21 19 5 30 9
样例输出 1 2 3 5 4
评测用例规模与约定 对于30%的评测用例,1 ≤ N , K ≤ 10, 1 ≤ w ≤ N , 1 ≤ s , c ≤ 30; 对于60%的评测用例,1 ≤ N , K ≤ 50,1 ≤ w ≤ N ,1 ≤ s ≤ 300,1 ≤ c ≤ 50; 对于所有评测用例,1 ≤ N , K ≤ 1000,1 ≤ w ≤ N ,1 ≤ s ≤ 10000,1 ≤ c ≤ 100。
分析 用到两个结构体,保存每次取用的3个数据分别按取出时间和返还时间的排序,即按时间顺序排列。
vector使用sort排序时写法是sort(v.begin(),v.begin()+v.size());
。
code 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 89 90 #include <bits/stdc++.h> using namespace std;struct Node { int no,s,en; }node[1005 ],node1[1005 ]; bool cmp1 (Node const &a, Node const &b) { if (a.s==b.s){ return a.no<b.no; } return a.s<b.s; } bool cmp2 (Node const &a, Node const &b) { if (a.en==b.en){ return a.no<b.no; } return a.en<b.en; } int order[1005 ]; int ans[1005 ]; vector<int > v; int main () { int N,K; scanf ("%d%d" ,&N,&K); for (int i=1 ;i<=N;i++){ order[i]=i; } for (int i=0 ;i<K;i++){ scanf ("%d%d%d" , &node[i].no, &node[i].s,&node[i].en); node[i].en += node[i].s; node1[i] = node[i]; } sort (node,node+K,cmp1); sort (node1,node1+K,cmp2); int f1=0 , f2=0 ; for (int time=1 ;!(f1==K && f2==K);time++){ for (int i=f1;i<K;i++){ if (node1[i].en > time) break ; if (node1[i].en == time){ order[node1[i].no] = v[0 ]; v.erase (v.begin ()); } f1++; } for (int i=f2;i<K;i++){ if (node[i].s > time)break ; if (node[i].s == time){ v.push_back (order[node[i].no]); sort (v.begin (),v.begin ()+v.size ()); order[node[i].no]=0 ; } f2++; } } for (int i=1 ;i<=N;i++){ ans[order[i]]=i; } for (int i=1 ;i<=N;i++){ cout<<ans[i]<<" " ; } return 0 ; }