L-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

 

这道题目的大致意思就是现在有n个学生,然后进寝室是要刷卡的,然后那个自动门在小于L秒内是不会被关闭的(此处不包括L秒),然后问你要刷卡才能进入的学生的数量,以及他们的序号(注意这里的序号是一开始Input中所给出的序号)。

一开始我卡在记录序号的地方,我用的是用两个for来判断,结果TLE。

用结构体啊,好东西,定义两个结构体,然后其中一个保存秒数,另一个保存的是序号。

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node{
	int time;
	int x;	//x代表的是每个学生的序号; 
}a[20005];
bool cmp(node a,node b){
	return a.time<b.time;
}
int main(){
	int T,i,j,k,n,l;
	int hh,mm,ss;
	int f[20005];
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&n,&l);
		memset(a,0,sizeof(a));  memset(f,0,sizeof(f));   k=1;
		for(i=1;i<=n;i++){
			scanf("%d:%d:%d",&hh,&mm,&ss);
			a[i].time=hh*3600+mm*60+ss;
			a[i].x=i;				//这里别忘了把编号也给存进去; 
		}
		sort(a+1,a+1+n,cmp);
		int m=a[1].time;
		//下面是关键;首先第一个人肯定要刷卡的 
		for(i=2;i<=n;i++){
			//如果i个人不满足if中的那个条件,那么就把m更新为i这个人的time,并把它的序号也存进去; 
			if(m+l<=a[i].time){
				f[k++]=a[i].x;		//保存要刷卡的那个人的序号; 
				m=a[i].time;
			}
		}
		//最后别忘了第一个人也是要刷卡的; 
		f[k]=a[1].x;
		sort(f+1,f+k+1);
		printf("%d\n",k);
		for(i=1;i<=k;i++){
			if(i!=k)  printf("%d ",f[i]);
			else printf("%d\n",f[i]);
		}
	}
}


 

*其实每个模拟题都能反应出你自己哪一方面的代码功底的欠缺,像我没有想到能用结构体保存编号(这样就不用for两遍去找原先的那个编号了),而且那个关键步骤我也不是能想的很清楚,所以继续努力,加油!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值