#P3562. 「BalticOI 2021 Day2」The Collection Game

「BalticOI 2021 Day2」The Collection Game

Description

Phew! You only narrowly escaped from prison after your disastrous debut as an art thief. A legal route into the art business seems to be a better idea after all. So, you decided to work as an art critic at the same museum where you were caught before.

This means that you visit the museum several times and produce an art review for each visit. An art review covers several pairs of rooms of the museum. For each pair, you compare the art of the two rooms during your visit and determine which room displays the art collection with the higher aesthetic value. In the end, however, you also want to survey the art in the entire museum. That is, using all your reviews, you want to rank all rooms of the museum by decreasing aesthetic value of the art collections displayed.

Because planning is the key, you decide in advance for each visit which pairs of rooms you will compare. Also, for the sake of diversity, no room should appear more than once in a single art review.

Unfortunately, your newly gained reputation in the art community proves to be an obstacle. Whenever you announce that you will compare a particular pair of rooms during your next visit, the museum might spontaneously swap the art collections of these two rooms. Your final survey and room ranking is supposed to be based on what is displayed in each room during your last visit.

Write a program that schedules no more than 𝑉𝑉 visits to the museum and, based on the resulting art reviews, computes a list of all rooms of the museum ordered by decreasing aesthetic value of the art collections displayed in each room at the time of your last visit.

Communication

This is a communication task. You must implement the function void solve(int N, int V ) where 𝑁𝑁 is the number of rooms of the museum, numbered 11 to 𝑁𝑁, and 𝑉𝑉 is the maximal number of visits you are allowed to make. For each testcase, this function is called exactly once. In the function, you can use the following other functions provided by the grader:

  • void schedule(int i, int j) schedules a comparison of rooms 𝑖𝑖 and 𝑗𝑗 during your next visit (1iN,ij)(1\le i\le N,i\not=j).Immediately after a call to this function, the museum can decide to swap the art collections of rooms 𝑖𝑖 and 𝑗𝑗.
  • vector<int> visit() visits the museum and performs all scheduled comparisons. This function returns an array with exactly one entry for each scheduled comparison since your last visit to the museum (that is, for each call to schedule since the last call to visit or the beginning of the program). The entry at index 𝑘𝑘 is 11 if the art in room 𝑖𝑖 has a higher aesthetic value than in room 𝑗𝑗, and 00 otherwise. Here, 𝑖𝑖 and 𝑗𝑗 are the rooms from the (𝑘+1)(𝑘 + 1)-th scheduled comparison.
  • void answer(vector⟨int⟩ 𝑟) publishes your list of all the rooms of the museum ordered by decreasing aesthetic value. 𝑟𝑟 must be an array of length 𝑁𝑁; its 𝑖𝑖-th entry is the room with the (𝑖+1)(𝑖 + 1)-th most aesthetic art collection during your last visit to the museum. You must call answer exactly once; your program will be automatically terminated afterwards.

If any of your function calls does not match the above format, if a room is passed more than once as a parameter to schedule between two calls to visit, or if you call visit more than 𝑉𝑉 times, your program will be immediately terminated and judged as Not correct for the respective testcase. You must not write anything to standard output, otherwise you may receive the verdict Security violation!.

If you use C++, you must include the file swaps.h in your source code. To test your program locally, you can link your program with sample_grader.cpp which can be found in the attachment for this task in CMS (see below for a description of the sample grader). The attachment also contains a sample implementation with additional explanations as swaps_sample.cpp.

If you use Python, you can find a sample implementation as swaps_sample.py in the attachment which also describes the interface for Python submissions.

Sample Interaction

Consider a testcase with 𝑁=4𝑁 = 4 and 𝑉=50𝑉 = 50 where initially the rooms are ordered 11, 22, 33, 44 by decreasing aesthetic value. At the beginning, the grader calls your function solve as solve(4, 50). Then, one possible interaction between your program and the grader could look as follows:

Your program Return value Explanation
schedule(1, 2) schedules to compare the art in rooms 11 and 22.
schedule(3, 4) schedules to compare the art in rooms 33 and 44, the museum swaps the art of rooms 33 and 44.
visit() {1,0}\{1, 0\} visits the museum and performs all comparisons; the art in room 11 and 44 has a higher aesthetic, value than in room 22 and 33 respectively.
schedule(2, 4) schedules to compare the art in rooms 22 and 44.
visit() {1}\{1\} visits the museum; the art in room 22 has a higher aesthetic value than in room 44.
answer({1, 2, 4, 3}) you are convinced that the rooms are ordered 11, 22, 44, 33 by decreasing aesthetic value.
the solution is correct and is accepted.

Note that the above queries are of course not sufficient to determine the order of the rooms with certainty: for example, the order 22, 11, 44, 33 is also consistent with all answers to visit. This order is obtained when starting from 44, 11, 22, 33 and swapping the art of rooms 22 and 44 after the last call to schedule.

Grader

The sample grader expects on standard input the numbers 𝑁𝑁 and 𝑉𝑉 as well as a list of 𝑁𝑁 integers, the rooms ordered by decreasing aesthetic value at the beginning of solve. Then, the grader writes to standard output a protocol of all grader functions called by your program. Whenever schedule(𝑖, 𝑗) is called, the grader expects on standard input the number 11 if the art collections of rooms 𝑖𝑖 and 𝑗𝑗 should be swapped at that point in time, or 00 otherwise. At the end, the grader writes one of the following messages to standard output:

  • Invalid input. The input to the grader via standard input was not of the above format.
  • Invalid schedule. The function schedule was called with invalid parameters.
  • Out of visits. The function visit was called more than 𝑉𝑉 times.
  • Invalid answer. The function answer was called with invalid parameters.
  • Wrong answer. The function answer was called with a wrong list of rooms.
  • No answer. The function solve terminated without calling answer.
  • Correct: v visit(s) used. None of the above cases occurred and the function visit was called 𝑣𝑣 times.

In contrast, the grader which is used for the evaluation of your submission will only output Not correct (for any of the above errors) or Correct. Both the sample grader and the grader used for evaluation will terminate your program automatically whenever one of the above errors occurs or if your program calls answer.

Constraints

We always have 1N5001 \le N \le 500 and 50V500050 \le V \le 5000

  • Subtask 1(5 points):V=5000V=5000 and the museum never swaps art collections.
  • Subtask 2(10 points):V1000V \ge 1000 and the museum never swaps art collections.
  • Subtask 3(5 points):N100N \le 100, V=5000V=5000.
  • Subtask 4(15 points):V=5000V=5000.
  • Subtask 5(15 points):V500V\ge 500.
  • Subtask 6(35 points):V100V \ge 100.
  • Subtask 7(15 points):V50V \ge 50.

Moreover, the following holds: In each of the subtasks 33 to 77 you get 60%60\% of the points awarded for the respective subtask if you solve all testcases in which the museum always puts the art collection with the higher aesthetic value into room 𝑖𝑖 for each call to schedule. Inside CMS this is shown as “Group 1” of the corresponding subtask.