Cars on Campus

Cars on Campus

Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out times and the plate numbers of the cars crossing the gate. Now with all the information available, you are supposed to tell, at any specific time point, the number of cars parking on campus, and at the end of the day find the cars that have parked for the longest time period.

Input Specification:
Each input file contains one test case. Each case starts with two positive integers N (≤ 1 0 4 10^{4} 104), the number of records, and K (≤8× 1 0 4 10^{4} 104) the number of queries. Then N lines follow, each gives a record in the format:

plate_number hh:mm:ss status

where plate_number is a string of 7 English capital letters or 1-digit numbers; hh:mm:ss represents the time point in a day by hour:minute:second, with the earliest time being 00:00:00 and the latest 23:59:59; and status is either in or out.

Note that all times will be within a single day. Each in record is paired with the chronologically next record for the same car provided it is an out record. Any in records that are not paired with an out record are ignored, as are out records not paired with an in record. It is guaranteed that at least one car is well paired in the input, and no car is both in and out at the same moment. Times are recorded using a 24-hour clock.

Then K lines of queries follow, each gives a time point in the format hh:mm:ss. Note: the queries are given in ascending order of the times.

Output Specification:
For each query, output in a line the total number of cars parking on campus. The last line of output is supposed to give the plate number of the car that has parked for the longest time period, and the corresponding time length. If such a car is not unique, then output all of their plate numbers in a line in alphabetical order, separated by a space.

Sample Input:

16 7
JH007BD 18:00:01 in
ZD00001 11:30:08 out
DB8888A 13:00:00 out
ZA3Q625 23:59:50 out
ZA133CH 10:23:00 in
ZD00001 04:09:59 in
JH007BD 05:09:59 in
ZA3Q625 11:42:01 out
JH007BD 05:10:33 in
ZA3Q625 06:30:50 in
JH007BD 12:23:42 out
ZA3Q625 23:55:00 in
JH007BD 12:24:23 out
ZA133CH 17:11:22 out
JH007BD 18:07:01 out
DB8888A 06:30:50 in
05:10:00
06:30:50
11:00:00
12:23:42
14:00:00
18:00:00
23:59:00

Sample Output:

1
4
5
2
1
0
1
JH007BD ZD00001 07:20:09

The personal thought chart is as follows:

First-----Data
(1)
  struct car{
    …
    …
    …
  }
(2)
  Sort
   1 (.dic)plate
   2 time
   Insertion Sort
   Shell Sort
   Heap Sort
   Merge Sort
   Quick Sort
   Table Sort
   Bucket Sort
(3)
  Judge
   in with out

Second-----Count
(1)
 Sort
  time
  status 1 -1

Third-----Max
(1)
 Transform time
 Compare
 Invert time
 Sort (dic)plate
 Printf

AC code

/* Cars on Campus */
/* 该程序通过记录车牌号进出校门的时间来掌握某时刻校内车辆数量,以及全天停车时间最长的车辆 */

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct Cars *Car;
typedef struct Time *Tn;

struct Cars{
	char plate_number[8];//车牌号
	int time;//储存总秒数 
	int status;//记录状态 in - (1)    out - (-1)
};

struct Time{
	int hh;//时针-时刻
	int mm;//分针-时刻
	int ss;//秒针-时刻
};

void Read1( struct Cars L1[], int N);//车辆进出数据读入

void Read2( struct Time L2[], int K);//时间结点读入

void Plate_Sort( struct Cars L1[], int Left, int Right);//以字典顺序对plate_number排序

void Time_Sort( struct Cars L1[], int Left, int Right );//对相同的plate_number按time从小到大排序

void Judge( struct Cars L1[], int *N );//删除不配对信息,并更新数组长度 

void ParkingNumber( struct Cars L1[], struct Time L2[], int N, int K );//记录各时间结点number of Parking Cars,并予以输出

void MaxTime( struct Cars L1[], int N );//求解最长Park time,并对可能出现的多个数据排序,输出 

//----	main	----------------------------------
int main ()
{
	int N, K;//N--number of records   K--number of queries
	scanf("%d%d",&N, &K);
	int i=0, j;
	
	Car L1; Tn L2;
	/* 动态分配内存空间 */
	L1 = (Car)malloc(N * sizeof(struct Cars));//储存车辆进出数据
	L2 = (Tn)malloc(K * sizeof(struct Time));//储存时间结点
	
	Read1( L1, N );
	Read2( L2, K );
	
	Plate_Sort( L1, 0, N-1 );
	for(j=0;i<N;)
	{
		i=j;
		j=i+1;
		while(j<N && strcmp(L1[i].plate_number,L1[j].plate_number)==0)//找到相同plate_number的最大区间
			j++;
		if(i+1 != j) Time_Sort( L1, i, j-1);
	}
	
	Judge( L1, &N );//传递N的地址,更新新数组长度 
	
	/* 以再次调用Time_Sort(),便于ParkingNumber()运行 */ 
	Time_Sort( L1, 0, N-1 );//以Time为总排序,无plate_number限制
	ParkingNumber( L1, L2, N, K );
	
	/* 重新以plate_number首选,time次选排序 */
	Plate_Sort( L1, 0, N-1 );
	i=0;//再次调用,需要初始化
	for(j=0;i<N;)
	{
		i=j;
		j=i+1;
		while(j<N && strcmp(L1[i].plate_number,L1[j].plate_number)==0)
			j++;
		if(i+1 != j) Time_Sort( L1, i, j-1);
	}

	MaxTime( L1, N );
	
	return 0;
}
//----	main	----------------------------------


void Read1( struct Cars L1[], int N)
{
	int hh, mm, ss;//计算总时间
	char status[4];//输入status,比较stasus[0]
	for(int i=0;i<N;i++)
	{
		scanf("%s",L1[i].plate_number);
		scanf("%02d:%02d:%02d",&hh,&mm,&ss);
		L1[i].time= hh*3600 + mm*60 + ss;//将总时间以秒计量,方便Sort
		scanf("%s",status);
		if(status[0]=='i')L1[i].status=1;// in
		else L1[i].status= -1;// out
	}
}

void Read2( struct Time L2[], int K)
{
	for(int j=0;j<K;j++)
	{
		scanf("%02d:%02d:%02d", &L2[j].hh, &L2[j].mm, &L2[j].ss);
	}
}

void Plate_Sort( struct Cars L1[], int Left, int Right )
{
	if(Left >= Right)return;//出递归
	int i, j;
	i=Left; j=Right;
	struct Cars temp = L1[i];//temp做pivot 
	while(i!=j)
	{
		while(i<j && strcmp( temp.plate_number, L1[j].plate_number) <= 0)//找到 (> pivot) 停止 
			j--;
		if(i<j)L1[i]=L1[j];
		
		while(i<j && strcmp( temp.plate_number, L1[i].plate_number) >= 0)//找到 (< pivot) 停止 
			i++;
		if(i<j)L1[j]=L1[i];
	}
	L1[i]=temp;
	
	/* 因为以i做的pivot,所以递归以i-1 \ i+1 进行 */
	Plate_Sort( L1, Left, i-1 );
	Plate_Sort( L1, i+1, Right);
}

void Time_Sort( struct Cars L1[], int Left, int Right )
{
	if(Left >= Right)return;//出递归 
	int i, j;
	i=Left; j=Right;
	struct Cars temp = L1[i];//temp做pivot 
	while(i!=j)
	{
		while(i<j && temp.time <= L1[j].time )//找到 (> pivot) 停止 
			j--;
		if(i<j)L1[i]=L1[j];
		
		while(i<j && temp.time >= L1[i].time )//找到 (< pivot) 停止 
			i++;
		if(i<j)L1[j]=L1[i];
	}
	L1[i]=temp;
	
	/* 因为以i做的pivot,所以递归以i-1 \ i+1 进行 */
	Time_Sort( L1, Left, i-1 );
	Time_Sort( L1, i+1, Right);
}

void Judge( struct Cars L1[], int *N )
{
	int i, n=0;//n记录新数组长度 
	for(i=0;i < *N-1;i++)
	{
		if((L1[i].status==1 && L1[i+1].status==-1) && strcmp(L1[i].plate_number,L1[i+1].plate_number)==0){//前后配对 
			L1[n]=L1[i];
			L1[n+1]=L1[i+1];
			n+=2;
			i+=1;//与for里面的i++一起,前进两个单位,减少重复次数
		}
	}
	*N=n;//更新数组长度
}

void ParkingNumber( struct Cars L1[], struct Time L2[], int N, int K )
{
	int i=0, j, new_time;//new_time为L2[j]时刻总时间(s为单位)
	int count=0;//记录该时刻number of Parking Cars
	/*因为题目所给 acceding order, 所以将i放于外循环,且无需重置count值,累加即可*/
	for(j=0;j<K;j++)
	{
		new_time=L2[j].hh*3600 + L2[j].mm*60 + L2[j].ss;//化为总秒数,便于比较 
		while(i < N)
		{
			if(L1[i].time>new_time)break;//退出循环,打印count,更新new_time值 
			if(L1[i].status==1)count++;// in
			else count--;// out
			i++;
		}
		printf("%d\n",count); 
	}
}

void MaxTime( struct Cars L1[], int N )
{
	int max=0, time;//最大time计入max,time为(out-in)时间差
	int i, count=0;//count记录max的cars'number,若max更新,则count归零 
	int hh, mm, ss;//将总秒数转化为时、分、秒表示 
	for(i=0;i<N-1;i+=2)//数据经过Judge()已配对,i每次跳跃2个单位长度,i<N-1确保不会超出数组容量
	{
		time=0;
		while(1)
		{
			time += L1[i+1].time-L1[i].time;
			if(i+1!=N-1 && strcmp(L1[i].plate_number,L1[i+2].plate_number)==0) i+=2;//连续进出
			else//结束连续
			{
				if(max<time){//更新max值 
					max=time;
					count=0;
					L1[count]=L1[i];//MaxTime()在全程序最后执行,无需构建新数组存储数据 
				}
				else if(max==time){
					count++;
					L1[count]=L1[i];
				}
				break;
			}
		}
	}
	hh = max / 3600;
	mm = max / 60 % 60;
	ss = max % 60;
	for(i=0;i<=count;i++)//新结构数组已被排序,只需按顺序输出,无需再次排序 
		printf("%s ",L1[i].plate_number);
	printf("%02d:%02d:%02d",hh,mm,ss);
}

第一次做出PAT(甲级),别人考场上的题硬是给我做了几十个小时。总体来说,还算不错,见识到了自己与他人的距离。
明白目标,奋力前行!
作为第一次,记录一下,以后再想放弃的时候,原来我可以!
在这里插入图片描述
代码人 ~ 代码魂 ~ 此生钟爱代码神 ~ Big_Kiss~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值