OJ Question Record: Counter Service Question Number: 453

Counter Service Question Number: 453

Question request:
Write a program for receiving customers at a store counter. Now there is only one counter that can receive guests. Only one guest can be received at the same time. It takes a certain amount of service time to receive each guest. Now there is a table to record the arrival of customers. Time and the service time that each customer will occupy. If the counter is receiving guests when the customer comes or there are waiting customers in front of the customer, the customer needs to wait, and you need to calculate the maximum time the customer waits.

Input description There
are multiple groups of input data.

For each group of input data, the first line is an integer n (1<=n<=50), which means there are n customers. The second line is n integers c[n], each customer arrives at the time c i , c[n] is a non-decreasing sequence. The third line is n integers t[n], corresponding to the second line, it takes t[i] (1<=t[i]<=15) minutes for the customer to receive the service.

Output description
Each group of data corresponds to one line of output, which is the longest waiting time.

Input sample
3
3 3 9
2 15 14
1
182
11
Output sample
11
0

Problem-solving ideas: The
main thing is to understand how to calculate the waiting time of the current customer.
The waiting time of the first customer must be 0.
Except for the first customer, the length of time that other customers need to wait can be judged by the time of his arrival and the time of the last customer leaving (that is, waiting for completion and receiving service completion): if the current customer arrives less than the previous Customer departure time, that is, the current customer arrives before the previous customer has left, the current customer needs to wait, otherwise there is no waiting, the waiting time is 0, and the current customer needs to wait for the time = the last customer left time-current customer Time of arrival. That
is, the length of time the current customer needs to wait =
(the arrival time of the last customer + the time that the previous customer waited + the time that the previous customer received the service)-the time the current customer arrived.

Understand the main calculation ideas, and then the code implementation of the program.
We can use an array or structure to store the arrival time and the duration of the service received for each customer respectively when entering. Then calculate the length of time each member needs to wait, and store it in another result array. Just output the maximum value in the result array element.

Customs clearance code:

#include <iostream>

using namespace std;

int main() {
    
    
	int n;
	int time[51];
	int duration[51];
	int needWait[51];
	
	while (cin >> n) {
    
    
		
		for (int i = 0; i < n; i++) {
    
    
			cin >> time[i];
		}
		for (int i = 0; i < n; i++) {
    
    
			cin >> duration[i];
		}	
		
		needWait[0] = 0;
		
		for (int i = 1; i < n; i++) {
    
    
			if ((time[i - 1] + duration[i - 1] + needWait[i - 1] - time[i]) > 0) {
    
    
				needWait[i] = time[i - 1] + duration[i - 1] + needWait[i - 1] - time[i];
			} else {
    
    
				needWait[i] = 0;
			}
		}
		
		for (int i = 1; i < n; i++) {
    
    
			if (needWait[i] > needWait[0]) {
    
    
				needWait[0] = needWait[i];
			}
		}
		
		cout << needWait[0] << endl;		
	}
	
	return 0;
}

complete.

Guess you like

Origin blog.csdn.net/weixin_45711556/article/details/108534857