#P17143. [NOI 2026] 中位数
[NOI 2026] 中位数
Background
The statement and sample attachments come from QOJ。
When submitting to Luogu, there is no need to include the header #include "median.h"。Just copy
void init(int c, int t);
int median(int n, int k, std::vector<int> a);
to the beginning of your program, and compile with a C++17 or higher compiler。
Problem Description
For a multiset of size , let the result of sorting all its elements from large to small be 。Define its median as the -th largest number, i.e., $\operatorname{Median}(S)=y_{\left\lceil\frac{m}{2}\right\rceil-1}$。Note: the definition of median in this problem may be different from the usual definition。
Given a sequence of length , , and a positive integer ()。Define a partition as follows: choose an increasing index sequence of length , , which partitions the original sequence into segments, with index intervals (in order) 。
For a partition, define its balance value as follows: for each segment in the partition, compute the median of the multiset formed by all elements in that segment; then the median of the multiset of these medians is the balance value of this partition。Formally, for a partition , let and 。Let the median of the multiset of all elements in the -th segment () be $c_i=\operatorname{Median}(\{a_{b_i},a_{b_i+1},\ldots,a_{b_{i+1}-1}\})$。Then the balance value of this partition is 。
Please find the maximum balance value among all partitions。
【Implementation Details】
Contestants do not need to, and should not, implement the main function。
Contestants need to ensure the submitted source file includes the header median.h, i.e., add the following code at the beginning:
#include "median.h"
Contestants need to implement the following two functions in the submitted source file median.cpp:
void init(int c, int t);
- represent the test point ID and the number of testdata groups, respectively。 means this test point is the sample。
- For each test point, this function will be called by the grader exactly once when the program starts running。
int median(int n, int k, std::vector<int> a);
- represent the sequence length, the number of segments in the partition, and the given sequence, respectively。
- This function should return the maximum balance value。
- For each test point, this function will be called by the grader exactly times。
template_median.cpp in this problem directory is the provided sample code。Contestants may refer to it and implement their own code。
Input Format
【Grader Program Method】
Contestants can compile an executable in this problem directory using the following command:
g++ grader.cpp median.cpp -o median -O2 -std=c++14 -static
For the compiled executable file median:
- The executable will read input from standard input in the following format:
- The first line contains two non-negative integers 。
- Then follow the testdata groups in order。For each testdata group:
- The first line contains two positive integers 。
- The second line contains positive integers 。
- The executable will output to standard output in the following format:
- For each testdata group, output one line containing one positive integer, representing the maximum balance value。
0 2
10 4
6 5 1 9 2 3 10 7 4 8
10 5
5 7 3 10 8 2 9 1 6 4
9
8
Hint
【Sample Explanation】
For the first testdata group, one partition with the maximum balance value is ,,。It partitions the original sequence into segments 、、、。The medians of the elements in the segments are respectively, so the balance value of this partition is 。
For the second testdata group, one partition with the maximum balance value is ,,,。It partitions the original sequence into segments 、、、、。The medians of the elements in the segments are respectively, so the balance value of this partition is 。
【Sample 】
See median/median2.in and median/median2.ans in the contestant directory。
This sample satisfies the constraints of test point 。
【Sample 】
See median/median3.in and median/median3.ans in the contestant directory。
This sample satisfies the constraints of test point 。
【Sample 】
See median/median4.in and median/median4.ans in the contestant directory。
This sample satisfies the constraints of test point 。
【Sample 】
See median/median5.in and median/median5.ans in the contestant directory。
This sample satisfies the constraints of test points 。
【Constraints】
Let be the sum of over all testdata within a single test point。For all testdata, we have:
- ;
- ,,;
- For all , we have 。
::cute-table{tuack} | Test point ID | | | | Special property | |:-:|:-:|:-:|:-:|:-:| | | | | | None | | | | | ^ | | | | ^ | ^ | ^ | None | | | | | ^ | | | | ^ | ^ | ^ | None | | | | | | ^ | | | ^ | ^ | | ^ | | | ^ | ^ | | ^ | | | ^ | ^ | | ^ | | | ^ | ^ | | ^ | | | | | | ^ | | | ^ | ^ | | ^ |
Special property : for all , we have 。
Translated by ChatGPT 5