pta cars on campus

lareina_leeq 2016-12-17 07:49:15
我的程序不知道为什么总是运行时间超时,怎么改啊?
pta上的OJ不能用STL只能用c写
问题就是pta上的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 (≤104), the number of records, and K (≤8×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 accending 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.



#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MaxRecord 10000
#define MaxQuery 80000
struct records{
char plate_number[8]; //plate numbr of the car
int time; //the intime or the outtime of the car
int in; //in=1means it is a record of car coming in, in=0means it is a record of car going out
int visit;
};
struct records Records[MaxRecord],ValidRecords[MaxRecord]; //first struct array to record the input car info, second to record the valid record

struct valid{
char plate_number[8]; //plate number of the valid info of the car
int park_time; //total paking time of the valid car
};
struct valid Valid[MaxRecord]; //struct array to record the valid total parking time of cars

int N; //number of records;
int K; //number of queries
int size=0; //the size of struct array Valid
int k=0;
int same_car[MaxRecord][MaxRecord]={0};
char* name[N];
//declaration of function
int ToSecond(int hour, int minute, int second);
int Find(char* pla_num);
int cmp1(const void *a, const void *b );
int cmp2(const void *a, const void *b );
int cmp3(const void *a, const void *b );

int main(){
scanf("%d",&N);
scanf("%d",&K);
int i, j,hour, minute, second, record,time,index,total,index2,m;
char pla_num[8];
char status[4];
//read in the records
for(i=0;i<N;i++){
scanf("%s %d:%d:%d %s", pla_num, &hour, &minute, &second, status);
time=hour*3600+minute*60+second;
strcpy(Records[i].plate_number,pla_num);
Records[i].time=time;
Records[i].visit=0;
if(status[0]=='i')
Records[i].in=1;
else if(status[0]=='o')
Records[i].in=0;
}
//sort the struct array Records in ascending order of time
qsort(Records, N, sizeof(struct records), cmp1);
//filter the valid records
i=0;
j=1;
size=0; //the size of struct array Valid
int in=-1;
int out=-1;
int size1=0;//the size of struct array ValidRecords
for(i=0;i<N;i++){
if(Records[i].visit==1)
continue;
for(j=0;j<N;j++){
if(Records[j].visit==1)
continue;
if(strcmp(Records[i].plate_number,Records[j].plate_number)==0&&Records[j].in==1){
in=j;
Records[j].visit=1;
}
else if(strcmp(Records[i].plate_number,Records[j].plate_number)==0&&Records[j].in==0&&in==-1){
Records[j].visit=1;
continue;
}
else if(strcmp(Records[i].plate_number,Records[j].plate_number)==0&&Records[j].in==0&&in!=-1){
ValidRecords[size1++]=Records[in];
ValidRecords[size1++]=Records[j];
Records[in].visit=1;
Records[j].visit=1;
strcpy(pla_num, Records[j].plate_number);
index=Find(pla_num);
if(index!=-1)
Valid[index].park_time=Valid[index].park_time+Records[j].time-Records[in].time;
else{
strcpy(Valid[size].plate_number,pla_num);
Valid[size].park_time=Records[j].time-Records[in].time;
size++;
}
out=-1;
in=-1;
}
}
in=-1;
out=-1;
}
//sort the Valid array in the order of total parking time
qsort(Valid, size, sizeof(struct valid), cmp2);
//ascending order of time
qsort(ValidRecords,size1,sizeof(struct records),cmp1);
//read in the queries and calculate the total num of car and print out
total=0;
j=0;
for(i=0;i<K;i++){
scanf("%d:%d:%d",&hour,&minute,&second);
time=hour*3600+minute*60+second;
while(j<size1&&ValidRecords[j].time<=time){
if(ValidRecords[j].in==1){
total++;
}
else {
total--;
}
j++;
}
printf("%d\n",total);
}
//print the platenumber of largest time
index=size-1;//the last element in valid array
int park_time=Valid[index].park_time;
index2=0;
name[index2++]=Valid[index].plate_number;
while(index>0){
if(park_time==Valid[--index].park_time)
name[index2++]=Valid[index].plate_number;
}
qsort(name, index2, sizeof(char *), cmp3);

for(i=0;i<index2;i++)
printf("%s ",name[i]);

hour=park_time/3600;
minute=park_time%3600/60;
second=park_time%60;
if(hour<10)
printf("0%d:",hour);
else
printf("%d:",hour);
if(minute<10)
printf("0%d:",minute);
else
printf("%d:",minute);
if(second<10)
printf("0%d\n",second);
else
printf("%d\n",second);
return 0;
}
//find whether the car is in the valid array, if so, return the index, if not, return -1
int Find(char* pla_num){
int i;
for(i=0;i<size;i++)
if(strcmp(Valid[i].plate_number,pla_num)==0)
return i;
return -1;
}
//cmp1
int cmp1(const void *a, const void *b ){
return (*(struct records*)a).time-(*(struct records*)b).time;
}
//cmp2
int cmp2(const void *a, const void *b ){
return (*(struct valid*)a).park_time-(*(struct valid*)b).park_time;
}
//cmp3
int cmp3(const void *a, const void *b ){
return strcmp(*(char**)a,*(char**)b);
}
...全文
517 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 2016-12-17
  • 打赏
  • 举报
回复
你的N是全局变量,初始为0,char* name[N]; 没这种用法

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧