Title Description:
There is a school where teachers share N classrooms. As required, all keys must be placed in a public key box. Teachers cannot bring keys home.Every time before class, the teacher finds the key of his class from the public key box to open the door. After class, he puts the key back into the key box.
The key box has N hooks, arranged in a row from left to right, to hang keys for N classrooms.There are no fixed hanging positions for a string of keys, but there are signs on the keys, so teachers don't confuse the keys.
Each time they pick up a key, the teachers will find the key they need to remove it without moving the other keys.Each time a key is returned, the teacher who returns it finds the empty hook on the far left and hangs the key on it.If more than one teacher returns the keys, they return them in order of key number from smallest to largest.If both the teacher and the key are available at the same time, the teachers will return all the keys before taking them out.
At the beginning of the day, keys are placed in key boxes numbered from smallest to largest.There are K teachers to attend the class. Give each teacher the keys he needs, the time to start the class and the length of time to start the class. Assuming that the end of the class is the time to return the keys, what is the order of the keys in the final key box?
Input Format
The first line of input contains two integers, N and K.
The next K lines, each with three integers w, s, c, indicate the key number to be used by a teacher, the time at which the class begins, and the length of the class.There may be multiple teachers who use the same key, but the time they use the key does not overlap.
Ensure that the input data meets the input format without checking the validity of the data.
Output Format
Output a line containing N integers separated by a space representing the key number hung on each hook.
sample input
5 2
4 3 3
2 2 7
sample output
1 4 3 2 5
Sample Description
The first teacher used the key of Classroom 4 from time 3 and used 3 units of time, so she returned the key at time 6.The second teacher started using the key at time 2 and used 7 units of time, so she returned the key at time 9.
The key status after each critical moment is as follows (X means empty):
1 X345 after moment 2;
1 X3X5 after moment 3;
143X5 after moment 6;
1 4325 after moment 9.
sample input
5 7
1 1 14
3 3 12
1 15 12
2 7 20
3 18 12
4 21 19
5 30 9
sample output
1 2 3 5 4
Measure use case size and conventions
For 30% of the test cases, 1 < N, K < 10, 1 < w < N, 1 < s, c < 30;
For 60% of the test cases, 1 < N, K < 50, 1 < w < N, 1 < s < 300, 1 < c < 50;
For all evaluation cases, 1 < N, K < 1000, 1 < w < N, 1 < s < 10000, 1 < c < 100.
Think: When I see this topic, my first thought is to solve by violence, which has proved to be correct.To determine what kind of data structure the general idea is to use, the title is time-based, whether the sequence is to be returned or borrowed at the same time, we determine to use Set to store the time, set has automatic sorting and no duplication, vector to store each borrowed or returned event, and array to store the key.The sequence of keys, which makes the borrowed or returned events into structures, contains the time and related key ids, sorts the Vectors, and then through the Set, finds the events at the corresponding time, and processes the events.
Code
#include <deque> #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <bits/stdc++.h> using namespace std; typedef struct T{ int _time;//The moment you get or to return the key int id;//Key number }; bool cmp(T a,T b){//Time first, number again if(a._time==b._time) return a.id<b.id; return a._time<b._time; } set<int> Set;//Time set, automatic sorting vector<T> ta;//Grab Key Sequence vector<T> re;//Key Return Sequence int main(){ int n,k; cin>>n>>k; while(k--){ int w,s,c; cin>>w>>s>>c; //w,s,c key number, start time and length of class int h=s+c; Set.insert(s); Set.insert(h); T t1; t1._time=s;t1.id=w; T r; r._time=h;r.id=w; ta.push_back(t1); re.push_back(r); } sort(ta.begin(),ta.end(),cmp); sort(re.begin(),re.end(),cmp); int arr[n];//Key sequence for(int i=1;i<=n;i++){ arr[i]=i; //Initialize key sequence } for(set<int>::iterator it=Set.begin();it!=Set.end();it++){ //For the same set time, process the return key first for(vector<T>::iterator it1=re.begin();it1!=re.end();it1++) { if(it1->_time==*it){ for(int i=1;i<=n;i++){ if(arr[i]==0){ arr[i]=it1->id; break;//Note that only one event is handled at a time, and a failure occurs with fewer break s, resulting in subsequent zeros being repeatedly assigned } } } else if(it1->_time>*it) break; } /*cout<<"The sequence after completion is "; for(int i=1;i<=n;i++){ cout<<arr[i]<<" "; //Initialize key sequence } cout<<endl;*/ for(vector<T>::iterator it1=ta.begin();it1!=ta.end();it1++) { if(it1->_time==*it){ for(int i=1;i<=n;i++){ if(arr[i]==it1->id){ arr[i]=0; break; } } } else if(it1->_time>*it) break; } /*cout<<"The sequence after borrowing is "; for(int i=1;i<=n;i++){ cout<<arr[i]<<" "; } cout<<endl;*/ } for(int i=1;i<=n;i++) cout<<arr[i]<<" "; return 0; }