L

来源:互联网 发布:大数据运维的原则 编辑:程序博客网 时间:2024/05/17 03:21

ZOJ 3787
Description
For security issues, Marjar University has an access control system for each dormitory building.The system requires the students to use their personal identification cards to open the gate if they want to enter the building.
The gate will then remain unlocked for L seconds. For example L = 15, if a student came to the dormitory at 17:00:00 (in the format of HH:MM:SS) and used his card to open the gate. Any other students who come to the dormitory between [17:00:00, 17:00:15) can enter the building without authentication. If there is another student comes to the dorm at 17:00:15 or later, he must take out his card to unlock the gate again.
There are N students need to enter the dormitory. You are given the time they come to the gate. These lazy students will not use their cards unless necessary. Please find out the students who need to do so.
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains two integers N (1 <= N <= 20000) and L (1 <= L <= 3600). The next N lines, each line is a unique time between [00:00:00, 24:00:00) on the same day.
Output
For each test case, output two lines. The first line is the number of students who need to use the card to open the gate. The second line the the index (1-based) of these students in ascending order, separated by a space.
Sample Input
3
2 1
12:30:00
12:30:01
5 15
17:00:00
17:00:15
17:00:06
17:01:00
17:00:14
3 5
12:00:09
12:00:05
12:00:00
Sample Output
2
1 2
3
1 2 4
2
2 3

题意:

每一个人进入宿舍都要打卡,第一个人进入要刷卡,如果第二个人的时间与第二个人进去的时间小于第一个人进去的时间加L,则第二个人不用刷了就可以进入,否则需要刷卡,让你输出刷卡的人数,并且和刷卡的次序!

思路:

将所有时间时间化为秒,然后用结构数组存放每位同学的编号和时间,然后对数组用时间排序,定义tt位当前迭代时间,再输出a[0].time后(因为a[0]同学必须刷卡)tt初始化为a[0].time+l,对于找到当前同学后,下一刷卡同学的时间应为当前同学时间+l后出现的第一位同学;细节:

对于下标要放入数组中排序输出,并且是第一次出现的同学需要刷卡!!

代码:

#include<bits/stdc++.h>using namespace std;struct stu{int num;int time;};bool comp(const stu &a,const stu & b){return a.time<b.time;}int main(){int n,l,i,t;int h,m,s; char c,cc;stu a[200005];cin>>t;while(t--){vector<int>b;cin>>n>>l;for(i=0;i<n;i++){cin>>h>>c>>m>>cc>>s;a[i].time=3600*h+60*m+s; a[i].num=i+1;}sort(a,a+n,comp);int tt=a[0].time+l;b.push_back(a[0].num);for(i=0;i<n;i++){if(a[i].time>=tt) { sum++;int ccc=a[i].num; b.push_back(ccc); tt=a[i].time+l;}}cout<<sum<<endl;sort(b.begin(),b.end());cout<<b[0];for(i=1;i<b.size();i++)cout<<" "<<b[i];cout<<endl;b.clear(); }return 0;}

心得:

我们在做这个题目时,费了好大的劲,改了又改,其实当你真的做出来时,又发现很简单,所以,下次做题时一定要思路清晰!!

找到问题的关键点!!


1 0
原创粉丝点击