Bajtazar has just bought a balance scale with a set of seven weights with masses of 1, 2, 3, 4, 5, 6, and 7 kilograms. Unfortunately, the weights do not differ in appearance, and none of them have any markings.
The set includes stickers with the numbers 1, 2, 3, 4, 5, 6, and 7, but no instructions on which sticker to place on which weight. For now, Bajtazar has placed the stickers as he saw fit (one sticker per weight), but ultimately he would like each sticker to represent the actual mass of the weight.
Bajtazar has come up with a brilliant idea on how to do this. He can place some weights on the scale pans and observe which way the scale tips. Will you help Bajtazar determine the correct assignment of stickers to weights using a small number of weighings?
Interaction
You must implement a program that solves Bajtazar's problem using the provided library (which simulates the scale). To use the library, include the following in your program:
- C/C++:
#include "waglib.h" - Python:
from waglib import liczba_testow, poloz_lewo, poloz_prawo, odloz, wazenie, odpowiedz
The library provides the following functions:
liczba_testow()– During a single execution of the program, you will need to help Bajtazar multiple times (for different sets of weights). This function returns an integer $t$, representing the number of test cases to consider in the given run. You may assume that in the sample test $t = 2$, and in the remaining tests $t = 5040$.poloz_lewo(x)– Places the weight with sticker $x$ ($1 \le x \le 7$) on the left scale pan.poloz_prawo(x)– Places the weight with sticker $x$ ($1 \le x \le 7$) on the right scale pan.odloz(x)– Places the weight with sticker $x$ ($1 \le x \le 7$) on the table (i.e., removes it from the scale pans). The functionspoloz_lewo,poloz_prawo, andodlozcan be called regardless of the weight's position before the call. If the weight was already in the position you are trying to set, the function call has no effect.wazenie()– Returns a number from the set $\{-1, 0, 1\}$, representing: left pan heavier, scale in balance, right pan heavier, respectively.odpowiedz(T)– This function allows you to submit the solution to Bajtazar's problem. It must be called exactly once for each test case. It takes an array $T[0..6]$ (provided asstd::vector<int>in C/C++ or a list in Python), which means that the weight on which Bajtazar temporarily placed the sticker with number $i$ weighs $T[i-1]$. After calling theodpowiedzfunction, the program automatically proceeds to the next test case (in particular, all weights that were on the scale are removed). After executing this function on the last test, the program will automatically terminate.
The library does not have to assign stickers to weights at the beginning of the interaction with your program. It may change the weights of the weights during the interaction, as long as the new weights are still consistent with the results returned by previous calls to the wazenie function.
Your program must not open any files or use standard input and output. The solution will be compiled with the library using the following commands:
- C++:
g++ -O3 -static waglib.cpp wag.cpp - Python:
python3 wag.py
Subtasks
Your program will be tested on one group of tests.
If $R$ is the maximum number of calls to the wazenie function that your program performed in a single test case, your solution will receive the following number of points:
| Number of calls | Number of points |
|---|---|
| $R \le 9$ | 100 points per test |
| $10 \le R \le 15$ | $40 + 10(15 - R)$ points per test |
| $16 \le R \le 25$ | $10 + 3(25 - R)$ points per test |
| $R \ge 26$ | 0 points per test (verdict Wrong Answer) |
Example program execution
Below is the execution of the program for the sample test, in which there are $t = 2$ test cases. Let $w_i$ denote the mass of the weight on which Bajtazar placed the sticker with number $i$.
In the first case, the stickers are placed correctly ($w_i = i$ for each $i$), and we manage to verify this using three calls to the wazenie function.
In the second case, we have $w_1 = 2, w_2 = 1, w_3 = 3, w_4 = 7, w_5 = 4, w_6 = 5$ and $w_7 = 6$. If we also use at most 9 calls to the wazenie function in this case, we will receive 100 points for this test.
| Call | Result | Description |
|---|---|---|
poloz_lewo(1) |
||
poloz_lewo(2) |
||
poloz_lewo(3) |
||
poloz_prawo(7) |
On the left pan 1, 2, 3; on the right 7 | |
wazenie() |
1 | $w_1 + w_2 + w_3 < w_7$, so it must be $\{w_1, w_2, w_3\} = \{1, 2, 3\}$ and $w_7 = 7$ |
odloz(1) |
||
odloz(7) |
||
poloz_prawo(4) |
On the left pan 2, 3; on the right 4 | |
wazenie() |
-1 | $w_2 + w_3 > w_4$ and $w_4 \ge 4$, so it must be $w_1 = 1$ and $w_4 = 4$ |
odloz(3) |
||
odloz(4) |
||
poloz_lewo(4) |
||
poloz_prawo(6) |
On the left pan 2, 4; on the right 6 | |
wazenie() |
0 | $w_2 + 4 = w_6$ and $w_2 \in \{2, 3\}$ and $w_6 \in \{5, 6\}$, so it must be $w_2 = 2, w_6 = 6$, and consequently $w_3 = 3$ and $w_5 = 5$ |
odpowiedz({1,2,3,4,5,6,7}) |
We answer correctly that $w_i = i$ for each $i$ and proceed to the new test case; pans are emptied | |
poloz_lewo(1) |
||
poloz_lewo(2) |
||
poloz_lewo(3) |
||
poloz_prawo(7) |
On the left pan 1, 2, 3; on the right 7 | |
wazenie() |
0 | $w_1 + w_2 + w_3 = w_7$, so it must be $\{w_1, w_2, w_3\} = \{1, 2, 3\}$ and $w_7 = 6$ or $\{w_1, w_2, w_3\} = \{1, 2, 4\}$ and $w_7 = 7$ |
| ... | ... | ... |
odpowiedz({2,1,3,7,4,5,6}) |
We answer correctly and the program terminates |
Experiments
Example incorrect solutions along with example libraries can be found in the dlazaw folder. The libraries may differ in behavior from those on the testing system and may not meet the problem's assumptions. They are only intended to show how to interact with the program.