CF1630B.Range and Partition

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Given an array aa of nn integers, find a range of values [x,y][x, y] (x≤yx \le y), and split aa into exactly kk (1≤k≤n1 \le k \le n) subarrays in such a way that:

  • Each subarray is formed by several continuous elements of aa, that is, it is equal to al,al+1,…,ara_l, a_{l+1}, \ldots, a_r for some ll and rr (1≤l≤r≤n1 \leq l \leq r \leq n).
  • Each element from aa belongs to exactly one subarray.
  • In each subarray the number of elements inside the range [x,y][x, y] (inclusive) is strictly greater than the number of elements outside the range. An element with index ii is inside the range [x,y][x, y] if and only if x≤ai≤yx \le a_i \le y.

Print any solution that minimizes y−xy - x.

给定一个包含 nn 个整数的数组 aa,请找出一个值域区间 [x,y][x, y](满足 x≤yx \le y),并将 aa 恰好划分为 kk(1≤k≤n1 \le k \le n)个子数组,使得:

  • 每个子数组由 aa 中若干连续的元素构成,即形如 al,al+1,…,ara_l, a_{l+1}, \ldots, a_r(其中 1≤l≤r≤n1 \leq l \leq r \leq n);
  • aa 中每个元素恰好属于一个子数组;
  • 在每个子数组中,落在区间 [x,y][x, y](含端点)内的元素个数严格大于落在该区间外的元素个数;元素 aia_i 落在区间 [x,y][x, y] 内当且仅当 x≤ai≤yx \le a_i \le y。

请输出任意一组使 y−xy - x 最小的解。

输入格式

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤3⋅1041 \leq t \leq 3 \cdot 10^4) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers nn and kk (1≤k≤n≤2⋅1051 \le k \le n \le 2 \cdot 10^5) — the length of the array aa and the number of subarrays required in the partition.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n) where aia_i is the ii-th element of the array.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052\cdot10^5.

输入包含多个测试用例。第一行包含一个整数 tt(1≤t≤3⋅1041 \leq t \leq 3 \cdot 10^4),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 kk(1≤k≤n≤2⋅1051 \leq k \leq n \leq 2 \cdot 10^5),分别表示数组 aa 的长度以及划分中所需子数组的个数。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤n1 \leq a_i \leq n),其中 aia_i 是数组的第 ii 个元素。

保证所有测试用例的 nn 值之和不超过 2⋅1052\cdot10^5。

输出格式

For each test case, print k+1k+1 lines.

In the first line, print xx and yy — the limits of the found range.

Then print kk lines, the ii-th should contain lil_i and rir_i (1≤li≤ri≤n1\leq l_i \leq r_i \leq n) — the limits of the ii-th subarray.

You can print the subarrays in any order.

对于每个测试用例,输出 k+1k+1 行。

第一行输出 xx 和 yy —— 所找到区间的左右端点。

随后输出 kk 行,其中第 ii 行应包含 lil_i 和 rir_i(满足 1≤li≤ri≤n1\leq l_i \leq r_i \leq n)—— 第 ii 个子数组的左右端点。

子数组的输出顺序可以任意。

输入输出样例

  • 输入#1

    3
    2 1
    1 2
    4 2
    1 2 2 2
    11 3
    5 5 5 1 5 5 1 5 5 5 1

    输出#1

    1 2
    1 2
    2 2
    1 3
    4 4
    5 5
    1 1
    2 2
    3 11

说明/提示

In the first test, there should be only one subarray, which must be equal to the whole array. There are 22 elements inside the range [1,2][1, 2] and 00 elements outside, if the chosen range is [1,1][1, 1], there will be 11 element inside (a1a_1) and 11 element outside (a2a_2), and the answer will be invalid.

In the second test, it is possible to choose the range [2,2][2, 2], and split the array in subarrays (1,3)(1, 3) and (4,4)(4, 4), in subarray (1,3)(1, 3) there are 22 elements inside the range (a2a_2 and a3a_3) and 11 element outside (a1a_1), in subarray (4,4)(4, 4) there is only 11 element (a4a_4), and it is inside the range.

In the third test, it is possible to choose the range [5,5][5, 5], and split the array in subarrays (1,4)(1, 4), (5,7)(5, 7) and (8,11)(8, 11), in the subarray (1,4)(1, 4) there are 33 elements inside the range and 11 element outside, in the subarray (5,7)(5, 7) there are 22 elements inside and 11 element outside and in the subarray (8,11)(8, 11) there are 33 elements inside and 11 element outside.

在第一个测试用例中,只能有一个子数组,且该子数组必须等于整个数组。若选择的范围为 [1,2][1, 2],则范围内有 22 个元素,范围外有 00 个元素;若选择的范围为 [1,1][1, 1],则范围内有 11 个元素(即 a1a_1),范围外有 11 个元素(即 a2a_2),此时答案无效。

在第二个测试用例中,可以选择范围 [2,2][2, 2],并将数组划分为子数组 (1,3)(1, 3) 和 (4,4)(4, 4):在子数组 (1,3)(1, 3) 中,范围内有 22 个元素(即 a2a_2 和 a3a_3),范围外有 11 个元素(即 a1a_1);在子数组 (4,4)(4, 4) 中,仅有 11 个元素(即 a4a_4),且该元素在范围内。

在第三个测试用例中,可以选择范围 [5,5][5, 5],并将数组划分为子数组 (1,4)(1, 4)、(5,7)(5, 7) 和 (8,11)(8, 11):在子数组 (1,4)(1, 4) 中,范围内有 33 个元素,范围外有 11 个元素;在子数组 (5,7)(5, 7) 中,范围内有 22 个元素,范围外有 11 个元素;在子数组 (8,11)(8, 11) 中,范围内有 33 个元素,范围外有 11 个元素。

输入解题思路,AI测评打分。不知道怎么写?

首页