[C + + programming problem] occupy seats

Keywords: C++ Algorithm data structure

[problem description] the classroom seats in sun's school can be reserved every day.
A person can occupy multiple seats and must have consecutive seats. If he can't occupy so many seats he requires, he won't want any more. In order to reduce the difficulty, seats are allocated from small to large according to the seat number each time, and seats are allocated by the first adaptation method.

[input form] multiple groups of data are input.
Each group of data input seat rows n, 0 < n < = 100 (the number of seats is equal, the seats are sorted from left to right in each row, and the rightmost seat in the first row and the first seat in the second row are regarded as continuous seats), m (0 < m < = min (100, n * n)) individuals.
Then enter k (0 < k < = 100), and finally enter k commands.
There are only two commands:
1.in id num (for ID, 0 < = ID < m, Num seats shall be occupied. If no consecutive num (0 < num < = 20) seats are occupied, the command is invalid)
2.out id (represents that id wants to release all the seats he previously occupied)
Note: if the id has occupied a seat before and has not been released, then its in command is invalid,
If id has not occupied a seat before, his out command is also invalid.

[output form] output yes or no for each in command. If the command is valid, output yes. If it is invalid, output No.
After yes no, there is only carriage return without any other characters.

[sample input]

4 10

9

in 1 7

in 2 3

in 3 3

in 3 3

in 4 3

out 2

in 5 6

out 3

in 5 6

[sample output]

yes
yes

yes

no

yes

yes

no

yes

yes

[ideas]

Turning the two-dimensional position into a one-dimensional array for storage and storing the information of [seat] and [person] is a big step~

[AC code]

 #include<iostream>
 #include<vector>
 #include<string>
 #include<stack> 
 #include<cmath>
 #include<algorithm>
 #include<set>
 #include<cstring>
 #include<iomanip> 
 #include<sstream>
 #include<map>
 using namespace std;
 
 struct person{
 	int is;   //Whether the seats are occupied, and record how many seats are occupied at the same time
	int st;   //Seat occupancy start position
 	int ed;   //Seat occupation end position
 	
 	person(){
 		is=0;
 		st=0;
 		ed=0;
	 }
	 
	void init(){  //Clear when the seat is released
		is=0;
 		st=0;
 		ed=0;
	}
 }p[10010];
 
 int sit[10010];   //The seats are continuous, so the position information is stored directly in a one-dimensional array
 
 int main(){
 	int n;
 	cin>>n;  //Number of seats n*n
	n=n*n; 
 	
 	int m;
 	cin>>m;   //The id of the person numbered 0~m-1 is also the subscript of person
 	
 	memset(sit,-1,sizeof(sit));  //The initialization array is - 1, which means no one has been seated (starting with 0 to prevent conflict)
 	
 	int t;
 	cin>>t;
 	while(t--){
 		string s;
 		cin>>s;
 		
 		if(s=="in"){
 			int id,num;
 			cin>>id>>num;
 			
 			if(p[id].is != 0){      //The seat has been occupied and can't be occupied any more
 				cout<<"no"<<'\n';
 				continue;
			 }
			 	 
			int flag=0;
			for(int k=1;k<=n;k++){
				while(sit[k]!=-1 && k<=n) k++;  //Find the first empty seat with - 1
					
				if(k>n) break;    //No empty seats were found
				
				int maxi=0;
				int st=k;	
				while(sit[st]==-1 && st<=n)  {   //Continuous maximum empty seat
					maxi++;
					st++;         //When using while, don't forget to increase the number of steps
				}
				
				 if(maxi>=num){        //Enough position
				 	p[id].is = num;
				 	p[id].st = k;
				 	p[id].ed = k + num -1;
				 	
				 	for(int i=k;i<=k+num-1;i++){  //Update location information 
				 		sit[i]=id;
					 }
					 
					 flag=1;
					 cout<<"yes"<<'\n'; 
					 break;
				 }
			} 
 			if(flag==0 )  cout<<"no"<<'\n'; //The largest continuous empty seat is not enough to occupy a seat
		 }
		 
 		else if(s=="out"){
 			int id;
 			cin>>id;
 			
 			if(p[id].is == 0){   //No seat per se 
 				cout<<"no"<<'\n';
 				continue;
			 } 
			 
			 for(int i=p[id].st ; i<=p[id].ed ;i++){  Empty seat
			 	sit[i]=-1;	
			 } 
			 
			p[id].init() ;			
			cout<<"yes"<<'\n';
		 }			
	 }	
 }

[write later]

If you think it's helpful to you, remember to press one button three times

Relevant topics are placed in the [HNU CJ] column

If you have any questions, you can comment or leave a message to me~

Posted by Mad_Mike on Sun, 05 Sep 2021 11:03:49 -0700