#P16454. [APIO 2026] Scallion Pancake Party

    ID: 16437 Type: RemoteJudge 4000ms 2048MiB Tried: 0 Accepted: 0 Difficulty: 8 Uploaded By: Tags>APIO交互题Special Judge2026通信题

[APIO 2026] Scallion Pancake Party

题目描述

Bohan is hosting a party. Since he loves scallion pancakes, he decides to buy them from the store nearby for his party. The store sells NN different flavors of scallion pancakes, numbered from 0 to N1N - 1. Bohan decides to buy NN scallion pancakes of each flavor, for a total of N2N^2 scallion pancakes. Each scallion pancake is divided into KK identical slices before put into its own bag. The bags are numbered from 0 to N21N^2 - 1. Let F[i]F[i] denote the scallion pancake flavor for bag ii.

During the party, Bohan invites all guests to play a game. The game proceeds as follows.

First, Bohan picks NN guests and brings them to rooms numbered 00, 11, \cdots, N1N - 1 such that each room has exactly one guest. The remaining guests stay outside until the end of the game. All NN rooms look exactly the same, so the guests cannot tell which room they are in.

Next, for each room ii (0i<N0 \le i < N), Bohan enters room ii and secretly tells the guest an integer P[i]P[i], representing a forbidden flavor. He may or may not also tell the guest which room they are in. Bohan guarantees that [P[0],P[1],,P[N1]][P[0], P[1], \cdots, P[N - 1]] is a permutation of [0,1,,N1][0, 1, \cdots, N - 1].

Then, there are NN rounds. In the ii-th round, Bohan brings bags 00, 11, \cdots, N21N^2 - 1 one by one in the sequential order into room i1i - 1. When the guest in room i1i - 1 receives bag jj, they learn:

  • F[j]F[j], the scallion pancake flavor that bag jj contains, and
  • the number of slices remaining in the bag.

Before receiving the next bag, the guest in room i1i - 1 must decide how many slices to eat from the current bag. The number of slices they can eat depends on the situation:

  • If P[i1]F[j]P[i - 1] \ne F[j], the guest can remove any number of slices from the bag and eat them.
  • If P[i1]=F[j]P[i - 1] = F[j], the guest is not allowed to eat any slices.

Note that the guests do not know the sequence F[0],F[1],,F[N21]F[0], F[1], \cdots, F[N^2 - 1] in advance.

After all NN rounds, the guests outside are given the sequence of bags. Upon seeing the bags, they learn the scallion pancake flavors and the number of slices left in each bag. With this information, they must determine the values of P[0],P[1],,P[N1]P[0], P[1], \cdots, P[N - 1].

The guests are allowed to communicate before the game starts. They also know the values of NN and KK in advance. Your goal is to implement a strategy so that the guests outside can always correctly determine the values of P[0],P[1],,P[N1]P[0], P[1], \cdots, P[N - 1].

Implementation details

You need to implement three procedures.

You should implement the following two procedures for guests in rooms 0,1,,N10, 1, \cdots, N - 1.

void init(int N, int K, int p, int r)

Suppose the guest is in room ii.

  • NN: the number of scallion pancake flavors.
  • KK: the number of slices of each scallion pancake before the game starts.
  • p=P[i]p = P[i]: the forbidden flavor to eat in room ii.
  • If Bohan decides to tell the guest which room they're in, then r=ir = i. Otherwise, r=1r = -1.
int strategy(int b, int f, int s)

Suppose the guest is in room ii.

  • bb: the current bag number.
  • f=F[b]f = F[b]: the scallion pancake flavor for bag bb.
  • ss: the number of slices left in bag bb.
  • This procedure should return a non-negative integer xx, representing the number of slices the guest decides to eat.
    • If fP[i]f \ne P[i], then 0xs0 \le x \le s must hold.
    • If f=P[i]f = P[i], then x=0x = 0 must hold.

You should implement the following procedure for the guests outside.

std::vector<int> guess(int N, int K, std::vector<int> F, std::vector<int> S)
  • NN: the number of scallion pancake flavors.
  • KK: the number of slices of each scallion pancake before the game starts.
  • FF: an array of length N2N^2 where F[i]F[i] is the flavor of the scallion pancake in bag ii.
  • SS: an array of length N2N^2 where S[i]S[i] is the number of slices remaining in bag ii after all NN rounds.
  • This procedure should return an array PP of length NN where P[i]P[i] is the number given to the guest in room ii.

Each test case involves TT independent games.

During the judging process, for each of the TT games, there will be N+1N + 1 processes. For each ii such that 0i<N0 \le i < N, process ii represents the guest in room ii. Process NN represents the guests outside. For each ii such that 0i<N0 \le i < N, process (i+1)(i + 1) starts after process ii finishes.

For processes 0 to N1N - 1:

  • init is called exactly once.
  • strategy is called N2N^2 times after init.
    • It is guaranteed that for the jj-th call, b=j1b = j - 1.

For process NN:

  • guess is called exactly once.

The judge may interleave processes from different games, but it is guaranteed that the relative order of processes within each individual game is preserved.

输入格式

The sample grader only supports one game per test case.

Input format:

N K
P[0] P[1] ... P[N-1]
F[0] F[1] ... F[N*N-1]
reveal
  • reveal is either 00 or 11.
    • If reveal is 00, the grader will act as if Bohan tells no one their room number.
    • If reveal is 11, the grader will act as if Bohan tells everyone their room number.

输出格式

If the array returned by guess matches [P[0],P[1],...,P[N1]][P[0], P[1], ..., P[N - 1]], the sample grader prints

Accepted

In addition, the grader prints the function calls made in order for debugging purposes.



提示

Example

Consider the situation in which T=1T = 1 and N=2,K=2,P=[1,0],F=[1,0,0,1]N = 2, K = 2, P = [1, 0], F = [1, 0, 0, 1] for the only game in the test case.

Let S[i]S[i] denote current number of slices remaining in bag ii. Initially, S=[2,2,2,2]S = [2, 2, 2, 2].

Suppose the guests determine the following strategy before the game starts.

  • For guests in rooms, if they are allowed to eat the current scallion pancake:
    • If the current scallion pancake is the first one they can eat, then they will eat all remaining slices.
    • Otherwise, they will only eat one slice.
  • For the guests outside:
    • If S=[0,0,1,1]S = [0, 0, 1, 1], then they will guess that P=[1,0]P = [1, 0].
    • Otherwise, they will guess that P=[0,1]P = [0, 1].

The game proceeds as follows.

First, the judge starts process 00 representing the guest in room 00 and calls init(2, 2, 1, -1). After initializing, the judge makes the following calls:

Procedure call Return value
strategy(0, 1, 2) 00
strategy(1, 0, 2) 22
strategy(2, 0, 2) 11
strategy(3, 1, 2) 00

The first and fourth calls must return 00 since P[0]=F[0]=F[3]=1P[0] = F[0] = F[3] = 1.

After this process ends (round 11 finishes), S=[2,0,1,2]S = [2, 0, 1, 2].

Next, the judge starts process 11 representing the guest in room 11 and calls init(2, 2, 0, -1). After initializing, the judge makes the following calls:

Procedure call Return value
strategy(0, 1, 2) 22
strategy(1, 0, 0) 00
strategy(2, 0, 1)
strategy(3, 1, 2) 11

Note that the second and third calls must return 00 since P[1]=F[1]=F[2]=0P[1] = F[1] = F[2] = 0.

After this process ends (round 22 finishes), S=[0,0,1,1]S = [0, 0, 1, 1].

Finally, the judge starts process 22 representing the guests outside. The judge makes the following call:

Procedure call Return value
guess(2, 2, [1, 0, 0, 1], [0, 0, 1, 1]) [1,0]

The guests outside guessed the correct permutation. Thus, the test case is considered correct.

Note that this approach does not always make it possible for the guests outside to guess the permutation correctly.

The attachment package for this task contains a different sample input in addition to this example.

Constraints

  • 1T4001 \le T \le 400.
  • 2N302 \le N \le 30.
  • The sum of N3N^3 over all games in a test case doesn't exceed 2700027\,000.
  • 1KN1 \le K \le N
  • K{1,3,N}K \in \{1, 3, N\}.
  • [P[0],P[1],,P[N1]][P[0], P[1], \cdots, P[N - 1]] is a permutation of [0,1,,N1][0, 1, \cdots, N - 1].
  • 0F[j]<N0 \le F[j] < N for each jj such that 0j<N20 \le j < N^2.
  • ii appears exactly NN times in [F[0],F[1],,F[N21]][F[0], F[1], \cdots, F[N^2 - 1]] for each ii such that 0i<N0 \le i < N.
  • The judge is not adaptive, that is, [P[0],P[1],,P[N1]][P[0], P[1], \cdots, P[N - 1]] and [F[0],F[1],,F[N21]][F[0], F[1], \cdots, F[N^2 - 1]] are fixed before the first call to init is made.

Subtasks

Subtask Score Additional Constraints
11 88 K=1,N=2K = 1, N = 2.
22 77 K=1K = 1 and Bohan decides to tell everyone which room they're in.
33 1414 K=1K = 1 and $[F[i \cdot N], F[i \cdot N + 1], \cdots, F[i \cdot N + N - 1]]$ is a permutation of [0,1,,N1][0, 1, \cdots, N - 1] for each ii such that 0i<N0 \le i < N.
44 1818 K=NK = N.
55 2121 K=3,N3K = 3, N \ge 3.
66 3232 K=1K = 1.