#P16703. [SEATST 2026] 两场考试 / Two Exams

[SEATST 2026] 两场考试 / Two Exams

Problem Description

There are NN students in the class. Each student is assigned an ID from 00 to N−1N - 1 based on the current class ranking. That is, student ii (for all 0≤i≤N−10 \le i \le N - 1) currently has class rank ii. Here, rank 00 is the best, and rank N−1N - 1 is the worst.

The class has recently finished Chinese and Math exams. Student ii (for all 0≤i≤N−10 \le i \le N - 1) has rank A[i]A[i] in the Chinese exam and rank B[i]B[i] in the Math exam. Both AA and BB are permutations of length NN.

:::info[What is a permutation of length NN?]{open} In this problem, a permutation PP of length NN is an array of length NN such that for all 0≤i≤N−10 \le i \le N - 1, we have 0≤P[i]≤N−10 \le P[i] \le N - 1, and for all 0≤i<j≤N−10 \le i < j \le N - 1, we have P[i]≠P[j]P[i] \ne P[j].

For example, [2,1,0][2, 1, 0] is a permutation of length 33, but [1,2,3][1, 2, 3] and [2,0,2][2, 0, 2] are not permutations of length 33. :::

The teacher wants to re-rank all students. The new ranking can be represented by a permutation PP.

For each student ii, their new class rank must satisfy at least one of the following conditions:

  • For all jj such that P[j]<P[i]P[j] < P[i], student jj has a better Chinese score than student ii (i.e., A[j]<A[i]A[j] < A[i]), or
  • For all jj such that P[j]<P[i]P[j] < P[i], student jj has a better Math score than student ii (i.e., B[j]<B[i]B[j] < B[i]).

:::warning[Warning]{open} This condition only applies to those jj with P[j]<P[i]P[j] < P[i]. There are no restrictions for those jj with P[j]≥P[i]P[j] \ge P[i].

For each student ii, when checking whether the condition is satisfied, you must first choose one subject, and then compare student ii with all the corresponding students jj using that subject. For the same ii, all different jj must be better than student ii in the same subject. You cannot switch subjects halfway through when evaluating the condition for student ii. :::

The dissatisfaction of the new class ranking is defined as the maximum amount of rank drop among all students. In other words, dissatisfaction is the maximum value of P[i]−iP[i] - i (for all 0≤i≤N−10 \le i \le N - 1).

:::warning[Warning]{open} Dissatisfaction is the maximum of P[i]−iP[i] - i. The value of i−P[i]i - P[i] does not affect the calculation of dissatisfaction. :::

Among all possible new rankings, find the minimum possible dissatisfaction.

Implementation Details

You need to implement the following function:

int minimum_dissatisfaction(int N, std::vector<int> A, std::vector<int> B)
  • NN: the number of students.
  • AA: an array of length NN, representing the ranks in the Chinese exam.
  • BB: an array of length NN, representing the ranks in the Math exam.
  • This function should return the minimum dissatisfaction of the new class ranking.
  • This function is called exactly once for each testdata.

Input Format

N
A[0] A[1] ... A[N - 1]
B[0] B[1] ... B[N - 1]

Output Format

A single integer, which is the return value of minimum_dissatisfaction.

Hint

Sample

Consider the following function call:

minimum_dissatisfaction(5, [3, 0, 4, 1, 2], [0, 3, 2, 4, 1])

In this example, one way to assign the new ranking is P=[0,2,3,4,1]P = [0, 2, 3, 4, 1].

Consider student 11, with P[1]=2P[1] = 2. All students jj such that P[j]<P[1]P[j] < P[1] have a better Math rank than student 11, so this student satisfies the class ranking condition.

Next, consider student 22, with P[2]=3P[2] = 3. All students jj such that P[j]<P[2]P[j] < P[2] have a better Chinese rank than student 22, so this student also satisfies the class ranking condition.

It can be verified that all other students also satisfy the class ranking condition.

The dissatisfaction of this new ranking is 11. There is no other new ranking with lower dissatisfaction, so the function should return 11.

Constraints

  • 1≤N≤5 000 0001 \le N \le 5\ 000\ 000.
  • For all 0≤i≤N−10 \le i \le N - 1, 0≤A[i],B[i]≤N−10 \le A[i], B[i] \le N - 1.
  • For all 0≤i<j≤N−10 \le i < j \le N - 1, A[i]≠A[j]A[i] \ne A[j].
  • For all 0≤i<j≤N−10 \le i < j \le N - 1, B[i]≠B[j]B[i] \ne B[j].

Subtasks

  1. (33 points) N≤8N \le 8.
  2. (44 points) N≤20N \le 20.
  3. (1313 points) N≤500N \le 500.
  4. (1212 points) N≤3000N \le 3000, and for all 0≤i≤N−10 \le i \le N - 1, A[i]+B[i]=N−1A[i] + B[i] = N - 1.
  5. (1919 points) N≤3000N \le 3000.
  6. (1515 points) N≤100 000N \le 100\ 000, and for all 0≤i≤N−10 \le i \le N - 1, A[i]+B[i]=N−1A[i] + B[i] = N - 1.
  7. (1717 points) N≤100 000N \le 100\ 000.
  8. (1717 points) no additional constraints.

Note: For subtask 88, the judging program alone is guaranteed to take 15001500 milliseconds out of the 30003000 milliseconds time limit.

Translated by ChatGPT 5