L1-049. Ranked Seat Allocation

There are a large number of players participating in the ladder competition every year. It is necessary to ensure that all players in the same school cannot be adjacent to each other, and allocating seats has become a more troublesome thing. To this end, we formulate the following strategy: Suppose there are N schools participating in a game, and the ith school has M[i] teams, each with 10 players. Let the players of each school line up in a column, and the players of the i+1th team are ranked after the players of the ith team. Starting with the first school, the first team member from each school is seated, then the second team member from each school...and so on. If there are only 1 school teams left that have not been assigned seats, their team members will need to be seated in alternate seats. This question requires you to write a program to automatically generate the seat numbers of the players for each school, starting from 1.

Input format:

Enter the number N of participating colleges and universities in one line (a positive integer not exceeding 100); the second line gives N positive integers not exceeding 10, where the ith number corresponds to the number of participating teams in the ith college, and the number are separated by spaces.

Output format:

Starting from the first team of the first high school, output the seat numbers of the players in sequence. Each team occupies one line, the seat numbers are separated by a space, and there must be no extra spaces at the beginning and end of the line. In addition, the first line of each college presses "#X" to output the school's number X, starting from 1.

Input sample:
3
3 4 2
Sample output:
#1
1 4 7 10 13 16 19 22 25 28
31 34 37 40 43 46 49 52 55 58
61 63 65 67 69 71 73 75 77 79
#2
2 5 8 11 14 17 20 23 26 29
32 35 38 41 44 47 50 53 56 59
62 64 66 68 70 72 74 76 78 80
82 84 86 88 90 92 94 96 98 100
#3
3 6 9 12 15 18 21 24 27 30
33 36 39 42 45 48 51 54 57 60

#include <iostream>
#include <vector>
using namespace std;
vector<int> num[101]; // Contestant's seat number
int sum[101]; // The number of teams from each college
int flag[101]; // Mark whether the seats of each college player are full
int main() {
	int n, i, j, k = 0, h, max = 0, len = 0, t;
	memset(num, 0, sizeof(num)); // initialized to 0
	memset(sum, 0, sizeof(sum));
	memset(flag, 0, sizeof(flag));
	cin>>n;
	for (i = 0; i < n; ++i) {
		cin>>sum[i];
		if (max < sum[i])
			max = sum[i];
	}		
	max *= 10; // Calculate the number of participants from the school with the most teams
	if (n == 1) // test point 3: the case of only one school
		k = -1;
	for (j = 0; j < max; ++j) {// Take the maximum number of participants as the outer loop
		for (i = 0; i < n; ++i) {// Assign seats to n schools in turn each round
			if (flag[i]) // The i-th college is full, no operation
				continue;
			if (len == n - 1) {// When there is only one school left, take the next seat
				k += 2;
				num[i].push_back(k);
			} else if (num[i].size() < sum[i] * 10) //not full
				num[i].push_back(++k);
			else {// just full
				flag[i] = 1; // full flag is 1
				len++; // count: the number of full schools
			}			
		}		
	}	
	for (i = 0; i < n; ++i) {
		cout<<"#"<<i + 1;
		for (j = 0; j < num[i].size(); ++j) {
			if (j % 10 == 0) // output note format
				cout<<endl;
			else if (j)
				cout<<" ";
			cout<<num[i][j];
		}
		cout<<endl;
	}
	return 0;
}

Guess you like

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