ZOJ 3878 Access System
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
分析:
处理的时候统一把时间都换成用秒来计算。
再处理一下需要跨24小时的特殊情况就可以了。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 0x7fffffff
int vis[21000];
struct node
{
string s;
int id;
}tim[21000];
int cmp(node x,node y)
{
return x.s<y.s;
}
int main()
{
int t,N,L,cnt,ti,sum,f,h,xu[21000],num1;
while(cin>>t)
{
cnt=0;
while(t--)
{
num1=0;
f=0;
cnt=0;
memset(vis,0,sizeof(vis));
ti=INF;
cin>>N>>L;
for(int i=1;i<=N;i++)
{
cin>>tim[i].s;
tim[i].id=i;
}
sort(tim+1,tim+N+1,cmp);
for(int i=1;i<=N;i++)
{
sum=(tim[i].s[0]-'0')*10*60*60+(tim[i].s[1]-'0')*60*60+(tim[i].s[3]-'0')*10*60+(tim[i].s[4]-'0')*60+(tim[i].s[6]-'0')*10+(tim[i].s[7]-'0');
// cout<<ti<<endl;
// cout<<" "<<sum<<endl;
if(sum<ti)
sum+=60*60*24;
if(abs(sum-ti)>=L)
{
xu[cnt++]=tim[i].id;
ti=sum;
ti=ti%(24*60*60);
}
}
cout<<cnt<<endl;
sort(xu,xu+cnt);
for(int i=0;i<cnt;i++)
i==0?cout<<xu[i]:cout<<" "<<xu[i];
cout<<endl;
}
}
return 0;
}