#P9371. [APIO2023] 序列 / sequence
[APIO2023] 序列 / sequence
Background
Due to some bugs, submitting with C++14 (GCC9) will cause a compilation error. Please submit using C++14 and other languages.
When submitting, you do not need to include sequence.h. You need to implement the following function in your submitted code:
int sequence(int N, std::vector<int> A)
Problem Description
In the fascinating country of APIO, there lives a young and wise student, Alice. Alice has an insatiable curiosity for solving interesting problems that can challenge her mathematical ability. One day, she ran into difficulties while solving a mysterious problem about a sequence of length (i.e., ), and she could not resist the temptation to explore the answer.
Now, she would like to share some of her findings with you. However, for better understanding, we need the following definitions:
-
Define as , i.e., the number of occurrences of in .
-
Define the median set of a non-empty integer sequence as . Alice will show how to compute the median set step by step:
-
First, sort the sequence in non-decreasing order, and denote the sorted sequence as .
-
Then, $S(\{B[0], B[1] \cdots B[k-1]\})=\left\{C\left[\left\lfloor\frac{k-1}{2}\right\rfloor\right], C\left[\left\lceil\frac{k-1}{2}\right\rceil\right]\right\}$.
-
To better understand how is computed, here are some examples:
- .
- .
- .
-
As a challenging problem, Alice wants to find, for all with , the maximum value of . Here, represents the median set derived from (as mentioned earlier, ). Although Alice already has an answer, she needs to check whether it is correct, so she comes to you and hopes you can solve the problem by programming.
Implementation Details
You need to implement the following procedure:
int sequence(int N, std:: vector<int> A);
- : The length of the sequence .
- : An array of length , which is the sequence mentioned in the input.
- The function should return an integer, representing the maximum value among all valid .
- This function is called exactly once.
Input Format
The sample grader reads the input in the following format:
Line : .
Line : .
Output Format
The sample grader prints your answer in the following format:
Line : The return value of sequence.
7
1 2 3 1 2 1 3
3
9
1 1 2 3 4 3 2 1 1
2
14
2 6 2 5 3 4 2 1 4 3 5 6 3 2
3
Hint
Examples
Sample 1
Consider the following call:
sequence(7,{1,2,3,1,2,1,3});
The function should return .
In this sample, , , and , so the value of is .
It is easy to verify that has the largest value among all valid pairs .
Sample 2
Consider the following call:
sequence(9,{1,1,2,3,4,3,2,1,1});
The function should return .
Sample 3
Consider the following call:
sequence(14,{2,6,2,5,3,4,2,1,4,3,5,6,3,2});
The function should return .
Constraints
- .
- .
Subtasks
- (11 points): .
- (17 points): .
- (7 points): There exists an such that and .
- (12 points): .
- (13 points): (for all satisfying ).
- (22 points): .
- (18 points): No additional constraints.
Translated by ChatGPT 5