luogu#P5208. [WC2019] I 君的商店

[WC2019] I 君的商店

Background

Special Notice.

Some notes when submitting this problem on Luogu (if it differs from the original statement, please follow what is written here):

  1. Different from the original problem, you do not need, and should not, include the shop.h header file at the beginning of your program.
  2. When submitting, please add the following function declaration in your program:
int query(int *S, int nS, int *T, int nT);
  1. Only C++ (including C++, C++11, C++14, C++17) submissions are supported.

Problem Description

Mr. V, Mr. I, and Mr. Y are good friends.

Mr. I recently opened a shop. The shop has NN kinds of items (with IDs being integers from 0N10 \sim N - 1). Each kind of item has an unlimited supply. The unit price of each item is either 00 or 11.

Mr. V wants to know the price of every item. Through some supernatural power, he already knows that among these NN items, the number of items priced at 11 is exactly odd/even, and there is at least one item whose price is 11.

However, Mr. V does not want to ask Mr. I himself. He chooses the following method: he prepares ++\infty money for Mr. Y, and lets Mr. Y run errands for him. Each time, he specifies two non-empty item sets S,TS, T to Mr. Y (items within the same set must be pairwise distinct, i.e. each kind of item can appear at most once in each set). Mr. Y will go to the shop, buy the items in the two sets respectively, bring them back, and tell Mr. V which set has a larger total price. However, when the sums of the two sets are equal, Mr. Y will answer Mr. V according to Mr. I’s instructions.

Carrying many items is tiring, so we define the physical cost of one errand as S+T\texttt{S} + \texttt{T}, where S\texttt{S} denotes the number of items in set SS.

Your task is to write a program to help Mr. V decide how to make Mr. Y run errands in a reasonable way, so as to deduce the value of every kind of item. Mr. Y’s stamina is limited, so you cannot make him too tired, i.e. you cannot let his total physical cost exceed a preset threshold MM.

Implementation Details

You do not need, and should not, implement the main function. You only need to implement the following function:

  • find_price(task_id, N, K, ans)
  • Here task_id denotes the subtask ID (see Constraints and Rules). NN denotes the number of items, and the meaning of KK is:
    • If K=0K = 0, it means there are an even number of items with value 11.
    • If K=1K = 1, it means there are an odd number of items with value 11.
  • You need to store the computed item prices in the array ans[]\text{ans}[], where ans[i]\text{ans[i]} is the price of the item with ID ii.

You can make queries to the interactive library by calling the following function:

  • query(S, nS, T, nT)
  • Here nS=S,nT=T\text{nS} = \texttt{S}, \text{nT} = \texttt{T}. Arrays S [0(nS - 1)]\text{S [0\ldots (nS - 1)]} and T[0(nT - 1)]\text{T[0\ldots (nT - 1)]} describe the two sets respectively. You must ensure:
    • nS, nT>0\text{nS, nT} > 0.
    • $\forall 0 \le i < \text{nS} , 0 \le \text{S[i]} < N$.
    • $\forall 0 \le i < \text{nT} , 0 \le \text{T[i]} < N$.
      \forall means “for any”. For example, $\forall 0 \le i < \text{nS} , 0 \le \text{S[i]} < N$ means: “for any ii in [0,nS)[0, \text{nS}), S[i]\text{S[i]} is in [0,N)[0, N)”.
  • The time complexity of calling this function once is Θ(nS + nT)\Theta(\text{nS + nT}). It returns 00 or 11, and the meaning is:
    • If the sum of prices of items in set SS is larger, return 00.
    • If the sum of prices of items in set TT is larger, return 11.
    • Otherwise, return 00 or 11 according to some unknown rule.
  • As stated, we define the cost of one call as nS + nT\text{nS + nT}.

During evaluation, the interactive library may call find_price multiple times (no more than 1010 times). Each call represents a new price-guessing game, and all item prices will be reset.

Input Format

The executable will read input from standard input in the following format:

The first line contains an integer T(T100)T ( T \le 100 ), the number of test groups. For each group:

  • The first line contains two integers task_id,N\texttt{task\_id}, N.
  • The next line contains a binary string ss of length NN, where sis_i denotes the price of item ii. You need to ensure that at least one item has price 11.

After reading is finished, the interactive library will call the find_price function TT times.

Then the interactive library will check whether your function is correct each time. If all are correct, it will output Correct; otherwise, it will output the corresponding error message.

Hint

Scoring Rules

This problem is first subject to the same limits as non-interactive programming problems. For example, a compilation error will cause the entire problem to score 00 points; runtime error, exceeding the time limit, exceeding the memory limit, etc. will cause the corresponding test points to score 00 points. You may only access variables you define yourself and the variables provided by the interactive library and their corresponding memory space. Attempting to access other memory may cause a compilation error or runtime error.

Under the above conditions, in a test point, you get full score if and only if, in every call to find_price:

  • Every call to query follows the rules, and the sum of call costs does not exceed MM.
  • Your function correctly computes the array ans[]\text{ans[]}.

In a test point, if you do not get full score, then you get 00 points.

Subtasks

Let the upper bound of the total cost be MM, and denote the answer array as ans[]\text{ans[]}:

  • Subtask 1: N5,M=100N \le 5, M = 100.
  • Subtask 2: N103,M=106N \le 10^3, M = 10^6.
  • Subtask 3: N105,M=100N \le 10^5, M = 100. It is guaranteed that i<j<k\forall i < j < k, if ans[i]=ans[k]\text{ans[i]} = \text{ans[k]} then ans[j]=ans[i]\text{ans[j]} = \text{ans[i]}.
  • Subtask 4: N104,M=2×105N \le 10^4, M = 2 \times 10^5.
  • Subtask 5: N5×104,M=350100N \le 5 \times 10^4, M = 350100.
  • Subtask 6: N105,M=500100N \le 10^5, M = 500100.

Note

Mr. I may not be willing to let Mr. V know the price of every item. When the sums are equal, he will answer in his own way.

Subtask Scores

In this contest, the score distribution of test points (or subtasks) depends on whether you are a CTT contestant. The score settings for this problem are as follows:

Subtask ID 1 2 3 4 5 6
CTT Score 2020 1111 99 1212 1717 3131
Non-CTT Score 3131 2121 1313 99 1111 1515

Luogu uses the CTT scoring scheme when judging.

Translated by ChatGPT 5