#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 S={x0,x1,…,xm−1}S=\{x_0,x_1,\ldots,x_{m-1}\} of size mm, let the result of sorting all its elements from large to small be y0≥y1≥⋯≥ym−1y_0\ge y_1\ge\cdots\ge y_{m-1}。Define its median Median⁡(S)\operatorname{Median}(S) as the ⌈m2⌉\left\lceil\frac{m}{2}\right\rceil-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 nn, [a0,a1,…,an−1][a_0,a_1,\ldots,a_{n-1}], and a positive integer kk (k≤nk\le n)。Define a partition as follows: choose an increasing index sequence of length k−1k-1, 0<b1<⋯<bk−1<n0<b_1<\cdots<b_{k-1}<n, which partitions the original sequence into kk segments, with index intervals (in order) [0,b1),[b1,b2),…,[bk−1,n)[0,b_1),[b_1,b_2),\ldots,[b_{k-1},n)。

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 kk medians is the balance value of this partition。Formally, for a partition b1,…,bk−1b_1,\ldots,b_{k-1}, let b0=0b_0=0 and bk=nb_k=n。Let the median of the multiset of all elements in the ii-th segment (0≤i<k0\le i<k) 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 Median⁡({c0,…,ck−1})\operatorname{Median}(\{c_0,\ldots,c_{k-1}\})。

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);
  • c,tc,t represent the test point ID and the number of testdata groups, respectively。c=0c=0 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);
  • n,k,an,k,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 tt 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 c,tc,t。
    • Then follow the testdata groups in order。For each testdata group:
      • The first line contains two positive integers n,kn,k。
      • The second line contains nn positive integers a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1}。
  • 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 11 Explanation】

For the first testdata group, one partition with the maximum balance value is b1=3b_1=3,b2=5b_2=5,b3=7b_3=7。It partitions the original sequence into 44 segments [6,5,1][6,5,1]、[9,2][9,2]、[3,10][3,10]、[7,4,8][7,4,8]。The medians of the elements in the segments are 5,9,10,75,9,10,7 respectively, so the balance value of this partition is Median⁡({5,9,10,7})=9\operatorname{Median}(\{5,9,10,7\})=9。

For the second testdata group, one partition with the maximum balance value is b1=2b_1=2,b2=4b_2=4,b3=6b_3=6,b4=8b_4=8。It partitions the original sequence into 55 segments [5,7][5,7]、[3,10][3,10]、[8,2][8,2]、[9,1][9,1]、[6,4][6,4]。The medians of the elements in the segments are 7,10,8,9,67,10,8,9,6 respectively, so the balance value of this partition is Median⁡({7,10,8,9,6})=8\operatorname{Median}(\{7,10,8,9,6\})=8。

【Sample 22】

See median/median2.in and median/median2.ans in the contestant directory。

This sample satisfies the constraints of test point 66。

【Sample 33】

See median/median3.in and median/median3.ans in the contestant directory。

This sample satisfies the constraints of test point 99。

【Sample 44】

See median/median4.in and median/median4.ans in the contestant directory。

This sample satisfies the constraints of test point 1414。

【Sample 55】

See median/median5.in and median/median5.ans in the contestant directory。

This sample satisfies the constraints of test points 18∼2018\sim20。

【Constraints】

Let NN be the sum of nn over all testdata within a single test point。For all testdata, we have:

  • 1≤t≤201\le t\le20;
  • 5≤n≤1065\le n\le10^6,2≤k≤n2\le k\le n,N≤106N\le10^6;
  • For all 0≤i<n0\le i<n, we have 1≤ai≤n1\le a_i\le n。

::cute-table{tuack} | Test point ID | N≤N\le | n≤n\le | kk | Special property | |:-:|:-:|:-:|:-:|:-:| | 1,21,2 | 4040 | 2020 | ≤n\le n | None | | 3∼53\sim5 | 800800 | 8080 | ^ | AA | | 66 | ^ | ^ | ^ | None | | 7,87,8 | 80008000 | 800800 | ^ | AA | | 99 | ^ | ^ | ^ | None | | 1010 | 2×1052\times10^5 | 2×1052\times10^5 | =2=2 | ^ | | 1111 | ^ | ^ | =3=3 | ^ | | 12,1312,13 | ^ | ^ | =5=5 | ^ | | 1414 | ^ | ^ | ≤10\le10 | ^ | | 1515 | ^ | ^ | ≡0(mod2)\equiv0\pmod 2 | ^ | | 16,1716,17 | 10610^6 | 10610^6 | >5>5 | ^ | | 18∼2018\sim20 | ^ | ^ | ≤n\le n | ^ |

Special property AA: for all 0≤i<n0\le i<n, we have ai≤2a_i\le2。

Translated by ChatGPT 5