#P12570. [UOI 2023] An Array and Medians of Subarrays

    ID: 14137 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 4 上传者: 标签>线段树二分树状数组2023UOI(乌克兰)

[UOI 2023] An Array and Medians of Subarrays

题目描述

Let's call the median of an array of length (2k+1)(2 \cdot k + 1) the number that appears in position (k+1)(k + 1) after sorting its elements in non-decreasing order. For example, the medians of the arrays [1][1], [4,2,5][4,2,5], and [6,5,1,2,3][6,5,1,2,3] are 11, 44, and 33, respectively.

You are given an array of integers aa of even length nn.

Determine whether it is possible to split aa into several subarrays of odd length such that all the medians of these subarrays are pairwise equal.

Formally, you need to determine whether there exists a sequence of integers i1,i2,,iki_1, i_2, \ldots, i_k such that the following conditions are satisfied:

  • 1=i1<i2<<ik=(n+1)1 = i_1 < i_2 < \ldots < i_k = (n + 1);
  • $(i_2 - i_1) \bmod 2 = (i_3 - i_2) \bmod 2 = \ldots = (i_k - i_{k - 1}) \bmod 2 = 1$;
  • $f(a[i_1..(i_2-1)]) = f(a[i_2..(i_3-1)]) = \ldots = f(a[i_{k - 1}..(i_k - 1)])$, where a[l..r]a[l..r] denotes the subarray consisting of elements al,al+1,,ara_l, a_{l + 1}, \ldots, a_r, and f(a)f(a) denotes the median of the array aa.

输入格式

The first line contains a single even integer nn (2n21052 \le n \le 2 \cdot 10^5) --- the length of the array.

The second line contains nn integers a1,a2,,ana_1,a_2,\ldots,a_n (1ai109)1\le a_i\le 10^9) --- the elements of the array.

It is guaranteed that nn is even.

输出格式

Output Yes\tt{Yes} if it is possible to divide aa into several subarrays of odd length in such a way that all medians of these subarrays are pairwise equal, and No\tt{No} otherwise.

4
1 1 1 1
Yes
6
1 2 3 3 2 1
Yes
6
1 2 1 3 2 3
No

提示

In the first example, the array [1,1,1,1][1,1,1,1] can be divided into the arrays [1][1] and [1,1,1][1,1,1] with medians equal to 11.

In the second example, the array [1,2,3,3,2,1][1,2,3,3,2,1] can be divided into the arrays [1,2,3][1,2,3] and [3,2,1][3,2,1] with medians equal to 22.

In the third example, the array [1,2,1,3,2,3][1,2,1,3,2,3] cannot be divided into several arrays of odd length with the same median values.

Scoring

  • (33 points): n=2n=2;
  • (1414 points): 1ai21 \le a_i \le 2 for 1in1\le i\le n;
  • (1212 points): aiai+1a_i \le a_{i+1} for 1i<n1\le i < n;
  • (1616 points): 1ai31 \le a_i \le 3 for 1in1 \le i \le n; each value occurs in aa no more than n2n \over 2 times;
  • (1717 points): n100n \le 100;
  • (1818 points): n2000n \le 2000;
  • (2020 points): no additional restrictions.