zoj 3787 Access System C implementation

First time blogging, so nervous--


This question means:

        There are N students who want to enter the dormitory. If the dormitory door is closed, they need to swipe their cards to enter. After one student swipes their cards, other students do not need to swipe their cards. Ask all the students how many students have swiped their cards after entering.


code show as below:

#include<stdio.h>

typedef struct timer{
	int time;
	int i;
} New;


int partition(Nove arr[],int low ,int high){ //Quick sort function
	Nine keys;
	key=arr[low];
	while(low<high){
		while(low<high&&arr[high].time>=key.time)
			high--;
		if(low<high)
			arr[low++]=arr[high];
		while(low<high&&arr[low].time<=key.time)
			low++;
		if(low<high)
			arr[high--]=arr[low];
			
	}
	arr[low]=key;
	return low;
}

void sort(Nove arr[],int start,int end){ //Quick sort function
	int pos;
	if(start<end){
		pos=partition(arr,start,end);
		sort(arr,start,pos-1);
		sort(arr,pos+1,end);
	}
}






int main(){
	int T,i,a,b,c,N,L,j;
	int time,sum,pr[20000];
	Nine arr [20000];
	scanf("%d",&T);
	while(T--){
		time=0;
		sum=0;
		scanf("%d %d",&N,&L);
		for(i=0;i<N;i++){
			scanf("%d:%d:%d",&a,&b,&c);
			arr[i].time=a*60*60+b*60+c;
			arr[i].i=i+1;
		}
		
	
	sort(arr,0,N-1); //The bubble sort was originally used but it timed out, and it was over when changed to quick sort


	int p=0;
	for(i=0;i<N;i++){
		if(arr[i].time>=time){ //The classmates after time enter to swipe the card
			sum++; //sum is the total number of card swipes
			pr[p]=arr[i].i; //pr[] records the number of the classmate who swiped the card
			p++;                        
			time=arr[i].time+L;     
		}
		
	}
	
	printf("%d\n",sum);
	
	for(i=0;i<sum;i++){
		for(j=i;j<sum;j++){
			if(pr[i]>pr[j]){
				int flag=pr[i];
				pr[i]=pr[j];
				pr[j]=flag;		
			}
		}
	}	
	
	for(i=0;i<sum;i++){
		printf("%d",pr[i]);
		if(i!=sum-1)
			printf(" "); //Note that the format is miserable. . . . .
	}
	
	printf("\n");
	
		
	}
	return 0 ;
} 	

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324580317&siteId=291194637
ZOJ
ZOJ