Single-queue multi-window service for bank queuing problems (25 points) (Structure sorting)

Suppose that the bank has K windows to provide services, and a yellow line is set in front of the window, and all customers line up in a long line after the yellow line according to the arrival time. When a window is free, the next customer will go to the window to process the transaction. When there are multiple windows to choose from, it is assumed that the customer always chooses the window with the lowest number.

This question requires to output the average waiting time, longest waiting time, and final completion time of N customers who came to wait for service, and count how many customers are served by each window.

Input format:
Enter the first line to give a positive integer N (≤1000), which is the total number of customers; then N lines, each line gives the arrival time T and transaction processing time P of a customer, and assumes that the input data has been reached The time is arranged in order; the last line gives a positive integer K (≤10), which is the number of business windows opened. It is assumed here that the maximum time for each customer's transaction to be processed is 60 minutes.

Output format:
In the first line, output the average waiting time (output to 1 digit after the decimal point), the longest waiting time, and the final completion time, separated by 1 space, and no extra space at the end of the line.

In the second line, output the number of customers served by each window in ascending order of number. The numbers are separated by a space, and there can be no extra spaces at the end of the line.

Input sample:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

Question idea:
Set up a structure to store the information used. Then establish the corresponding structure array to ensure "group order" and "overall order".

#include<iostream>
#include<string>
#include<map>
#include<algorithm>
using namespace std;

struct Student {
    
    
	string st;	//学号
	int score;	//分数
	int groupID;//考场号
	int grouprank;//在考场中的等级
	int allrank;//总排名
}student[30005];

bool cmp(Student a, Student b)
{
    
    
	if (a.score == b.score)		//分数相等,按学号增序
		return a.st < b.st;
	else
		return a.score > b.score;	//正常按分数从大到小
}

int main()
{
    
    
	int n;
	cin >> n;
	int sum = 0;
	for (int i = 1; i <= n; i++)
	{
    
    
		int k;
		cin >> k;
		for (int j = sum; j < sum + k; j++)
		{
    
    
			cin >> student[j].st >> student[j].score;
			student[j].groupID = i;
		}
		sort(student + sum, student + k + sum, cmp);
		student[sum].grouprank = 1;		//每个考场中第一名的排名是1
		for (int j = sum + 1; j < sum + k; j++)		//确定每个小组之间的排名
		{
    
    
			if (student[j].score == student[j - 1].score)
				student[j].grouprank = student[j - 1].grouprank;
			else
				student[j].grouprank = j-sum+1;
		}
		sum += k;		//算得总体人数

	}

	sort(student, student + sum, cmp);
	student[0].allrank = 1;		//和上面一致,分数最高的排名第一
	for (int j = 1; j < sum; j++)
	{
    
    
		if (student[j].score == student[j - 1].score)
			student[j].allrank = student[j - 1].allrank;
		else
			student[j].allrank = j+1;
	}
	cout << sum << endl;
	for (int i = 0; i < sum; i++)
	{
    
    
		cout << student[i].st << " " << student[i].allrank << " " << student[i].groupID << " " << student[i].grouprank << endl;
	}
	

	return 0;
}

Guess you like

Origin blog.csdn.net/xdg15294969271/article/details/114436723