#P16434. [APIO 2026 中国赛区] 蛋糕
[APIO 2026 中国赛区] 蛋糕
Background
When submitting, please choose a language standard higher than C++17, do not include the header cake.h, and copy the following code to the beginning of your program:
int compare_tastiness(std::vector<int> S1, std::vector<int> S2);
Problem Description
Lavi is a little star messenger who really likes eating cake. She believes that the tastiness of each cake can be defined as a positive integer.
One day, her good friend Sally bought a cake. The tastiness of this cake is a positive integer not greater than , but at this moment Lavi only knows the value of . Now Lavi wants to determine the value of .
Using the power of a star messenger, Lavi can make at most cakes, each with tastiness not exceeding , and tell Sally their tastiness values. Then Sally will secretly mix the bought cake into these cakes, and finally Sally will sort all these cakes in non-decreasing order of tastiness. Since cakes of different tastiness look exactly the same, Lavi cannot tell which one is the cake Sally bought. However, Sally has a very strong memory, so she clearly remembers the tastiness of every cake after sorting.
After sorting, Lavi can ask Sally the following query multiple times:
- Lavi chooses some cakes, splits them into two disjoint groups, and then Sally tells Lavi the comparison result between the sums of tastiness of the two groups.
Formally, suppose Lavi made cakes. Let be the tastiness values of all cakes after mixing in Sally’s bought cake and sorting. Each time, Lavi needs to provide two non-empty index sets , where and . Sally will tell Lavi the comparison result between and .
Now Lavi needs your help. But she reminds you that asking too many queries will put a big burden on Sally’s memory, so Sally sets a limit on the number of queries. Specifically, before Lavi makes cakes, Sally will give a positive integer . If Lavi makes more than queries, your score will decrease as the number of queries increases.
Please help Lavi decide how to bake the cakes and how to ask queries afterwards.
Implementation Details
Contestants do not need to, and should not, implement the main function.
Contestants need to ensure that the submitted program includes the header file cake.h, i.e., add the following code at the beginning of the program:
#include "cake.h"
Contestants need to implement the following two functions in the submitted source file cake.cpp:
std::vector<int> bake_cakes(int N, int W, int K);
- is the maximum number of cakes Lavi can bake.
- is the upper bound of the tastiness of Sally’s bought cake.
- is the query threshold.
- This function should return a positive integer array , representing the tastiness values of the cakes baked by Lavi, where:
- the length of must not exceed ;
- for all , we have .
- For each test case, this function will be called by the interaction library exactly once, and it will be called before all calls to
find_tastiness.
int find_tastiness(int m, int W, int K);
- is the number of cakes baked by Lavi, so the actual number of cakes after including Sally’s bought cake is .
- is the upper bound of the tastiness of Sally’s bought cake.
- is the query threshold.
- This function should return a positive integer , the tastiness of the cake bought by Sally as determined by Lavi.
- For each test case, this function may be called multiple times by the interaction library, and each call is independent.
In this function, you may call the following function:
int compare_tastiness(std::vector<int> S1, std::vector<int> S2);
- are the index sets of the two groups of cakes. You must ensure that are non-empty and all elements are distinct, i.e., and .
- This function returns an integer representing the comparison result between and , where means , means , and means .
- Within one call to
find_tastiness, you may call this function at most times.
The judge is not adaptive. The tastiness of Sally’s bought cake is already fixed before each call to find_tastiness.
Testing Program Usage
In the directory of this problem, contestants can compile into an executable using the following command:
g++ grader.cpp cake.cpp -o cake -O2 -std=c++14 -static
Input Format
For the compiled executable:
- The executable will read input data from standard input in the following format:
- The first line contains four positive integers .
- The second line contains positive integers , representing the tastiness of the cake bought by Sally for each call to
find_tastiness.
- You can enable the
-vor--verboseoption at runtime to print a more detailed interaction process. If this option is not enabled, the program will output, after each call tofind_tastiness, whether the returned value is correct and the number of calls tocompare_tastiness, and after all calls are finished, it will output the maximum number of calls tocompare_tastiness. If-vor--verboseis enabled, the program will additionally output:- the return value of
bake_cakes; - for each call to
compare_tastiness, the passed parameters, the compared information, and the comparison result.
- the return value of
40 20 5 3
19 7 20
Correct: found 19 in 4 compares
Correct: found 7 in 5 compares
Correct: found 20 in 5 compares
Correct. Max compare count is 5
Hint
Sample 1 Explanation
The interaction library will make the following call:
bake_cakes(40, 20, 5);
Lavi can bake at most cakes, the upper bound of the tastiness of Sally’s bought cake is , and the query threshold is .
One possible returned array is .
Next, the interaction library will make the following call three times:
find_tastiness(9, 20, 5);
In these three calls, the tastiness of Sally’s bought cake is , respectively.
- When the tastiness of Sally’s bought cake is , after sorting, the tastiness values of all cakes are . At this time,
- if you call
compare_tastiness([0, 2, 4], [1, 3, 5]), then the sum of tastiness in is , the sum of tastiness in is , so the function returns ; - if you call
compare_tastiness([8, 2, 6], [5, 0, 9]), then the sum of tastiness in is , the sum of tastiness in is , so the function returns ; - if you call
compare_tastiness([0, 4, 7], [1, 3, 6]), then the sum of tastiness in is , the sum of tastiness in is , so the function returns .
- if you call
- When the tastiness of Sally’s bought cake is , after sorting, the tastiness values of all cakes are . At this time,
- if you call
compare_tastiness([0, 1, 3], [6]), then the sum of tastiness in is , the sum of tastiness in is , so the function returns .
- if you call
Constraints
For all testdata:
- , , , .
- For all , .
::cute-table{tuack} | Test Point ID | Score | | | | | | :---: | :---: | :---: | :---: | :---: | :---: | | | | | | | | | | | | | | | | | | | | | | | | | | | | ^ |
Scoring
For any test case, if the return value of bake_cakes does not satisfy the constraints in the implementation details, or the calls to compare_tastiness do not satisfy the constraints in the implementation details, or the return value of find_tastiness is incorrect, then this subtask scores points.
For each test point, let be the maximum number of calls to compare_tastiness among all calls to find_tastiness in that test point. The score of the program is computed as follows:
- In test points , if , the score equals the full score of that test point; otherwise, the score is .
- In test point , the score is .
- In test point , the score is .
Translated by ChatGPT 5