Demo entry 6340558

code for C

   

Submitted by anonymous on Dec 28, 2016 at 13:06
Language: C. Code size: 4.0 kB.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node{
    int Time;
    char Num[8];
    char Oeration[4];
}Record[20000];//This array stores all the records which we input.
struct VNode{
    int InTime;
    int OutTime;
    char Num[8];
    int TotalTime;
}ValidRecord[20000];//This array stores all the valid records, including in time
                    //out time and its plate number, Total time = out time - in time.
int RecordNumber=0;// This variable stores the number of valid records.

void push(int i, char Num[8], int intime, int outtime);
int cmp1(const void*a,const void*b)//This function is used to compare the Plate Number(1st key), and time(2nd key).
{
    if(strcmp((*(struct Node*)a).Num, (*(struct Node*)b).Num)) return strcmp((*(struct Node*)a).Num, (*(struct Node*)b).Num);
    else return (*(struct Node*)a).Time - (*(struct Node*)b).Time;
}

int main(void){
    int N, K;
    int i, hh, mm, ss, CheckTime, Total, MaxTotal = -1;//hh refers to "Hour", mm refers to "Minute", ss refers to "second".
    int time[24 * 3600 + 100];//time array stores the number of cars on campus at any second of the day.
    scanf("%d%d",&N,&K);
    for (i=0;i<N;i++){
        scanf("%s %d:%d:%d %s", Record[i].Num, &hh, &mm, &ss, Record[i].Oeration);
        Record[i].Time = hh*3600+mm*60+ss;// We translate the hh:mm:ss to second, which can be helpful to sort and compare.
    }
    qsort(Record, N, sizeof(struct Node), cmp1);//sort the Records about Plate Number and time.
    for (i=0;i<N-1;i++)// Now we pick up the valid records of sorted records.Try to pair all the adjacent records.
        if (!strcmp(Record[i].Num, Record[i+1].Num) && Record[i].Oeration[0] == 'i' && Record[i+1].Oeration[0] == 'o' )
            push(RecordNumber++, Record[i].Num, Record[i].Time, Record[i+1].Time);
    memset(time, 0, sizeof(int)*(24*3600+100));//Set all the time point to 0.
    for (i=0;i<RecordNumber;i++){//Mark every points that have in/out operation,
                                //when in occurs,++ this point,when out occurs,-- this point.
        time[ValidRecord[i].InTime]++;
        time[ValidRecord[i].OutTime]--;
    }
    for (i=1;i<24*3600;i++)//Now scan the array to get the number of cars on campus at any time in the day.
        time[i] += time[i-1];
    for (i=0;i<K;i++){//Start to answer the request.
        scanf("%d:%d:%d", &hh, &mm, &ss);
        CheckTime = hh*3600+mm*60+ss;//We translate the hh:mm:ss to second, which can be helpful to sort and compare.
        printf("%d\n", time[CheckTime]);//Because we have stores all the time's number, so we just output it.
    }
    i=0;
    while (i<RecordNumber){//Then find out the longest time.
        Total = ValidRecord[i].TotalTime;
        while ((i<RecordNumber-1)&&(!strcmp(ValidRecord[i].Num, ValidRecord[i+1].Num)))//One car may come in and out many times in the same day.
            Total += ValidRecord[++i].TotalTime;
        if (Total>MaxTotal) MaxTotal = Total;//update the MaxTotal.
        i++;
    }
    i = 0;
    while (i<RecordNumber){// Then find out all the cars stay longest.
        Total = ValidRecord[i].TotalTime;
        while ((i<RecordNumber-1)&&(!strcmp(ValidRecord[i].Num, ValidRecord[i+1].Num)))//One car may come in and out many times in the same day.
            Total += ValidRecord[++i].TotalTime;
        if (Total == MaxTotal) printf("%s ", ValidRecord[i].Num);//Output the plate number of the car.
        i++;
    }
    printf("%02d:%02d:%02d",MaxTotal / 3600,MaxTotal % 3600 / 60, MaxTotal % 60);//Translates the time, and output it.
    return 0;
}//End of main.

void push(int i, char Num[8], int intime, int outtime){//We use push function to put the valid records into ValidRecord array.
    strcpy(ValidRecord[i].Num, Num);
    ValidRecord[i].InTime = intime;
    ValidRecord[i].OutTime = outtime;
    ValidRecord[i].TotalTime = outtime - intime;
}//End of Push.

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).