Marathon Ice Hockey
Time limit1sMemory limit64 MB
Assign each player a number of minutes by a fixed greedy order, then convert the resulting cyclic blocks into an explicit substitution list.
- Level
Medium7 of 10
- Topics
- Greedy, Sorting, Simulation, Implementation
- Solved
- No attempts yet
Problem
A marathon ice hockey game lasts minutes. During every minute of the game, exactly six players of Ante's team are on the ice.
Ante brought players to the tournament. Player has quality and endurance . The endurance is the total number of minutes that player may spend on the ice during the whole game, and those minutes do not have to be consecutive. If a player is on the ice for minutes, then rests on the bench, then plays more minutes, he has used minutes of his endurance. No player may ever be on the ice for more minutes than his endurance.
A substitution happens between two consecutive minutes, never inside a minute, and several players may be swapped at the same moment. A player who enters the ice at some moment cannot leave at that same moment, and a player who leaves cannot come back at that same moment.
The quality of the team during one minute is the sum of the qualities of the six players who are on the ice in that minute. is the sum of those values. For example, if the game lasts 3 minutes and the quality of the team is 15 in the first minute, 12 in the second and 14 in the third, then .
The input always allows at least one schedule that keeps six players on the ice in every minute, that is, .
Print the largest that Ante can reach, together with a schedule that reaches it. Many schedules reach the largest , so the output section pins down exactly one of them.
Marathon ice hockey has no goalie.
Input
The first line contains the integers and (, ), the length of the game in minutes and the number of players Ante brought.
Each of the next lines contains the integers and (, ), the quality and the endurance of one player. The players are numbered from 1 to in the order they are given in the input.
Output
The schedule is fixed by the rule below, so exactly one output is accepted.
Sort the players by quality in decreasing order, and players of equal quality by increasing number. Hand out playing time in that order: the next player in the order gets the smaller of his endurance and the number of minutes still unassigned, and player-minutes are handed out in total. Once minutes are assigned, every remaining player gets 0 minutes. This assignment reaches the largest , and is the sum of multiplied by the playing time of player .
Lay the schedule out in a row of cells numbered 1 to . Take the players whose playing time is at least 1 minute, in the same sorted order, and give each of them a block of consecutive cells as long as his playing time, filling the row from cell 1 without gaps. Cell belongs to minute . Every playing time is at most , so the six cells of one minute hold six different players, and those are the players on the ice in that minute.
The first line of output contains .
The second line contains the numbers of the six players who are on the ice in minute 1, in increasing order.
The third line contains , the number of substitution lines that follow.
Each of the next lines contains three integers , and , meaning that after minutes of play, player leaves the ice and player enters it.
Build the substitution lines this way. For each from 1 to , let be the players who are on the ice in minute but not in minute , sorted in increasing order, and let be the players who are on the ice in minute but not in minute , also sorted in increasing order. The two lists have the same length. Pair them by position and print one line per pair, with the groups printed in increasing order of .