click below for solution

Thursday, 27 January 2022

Range and Partition Solution|Codeforces round #768 (div 2)

                                            Range and Partition

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Given an array a of n integers, find a range of values [x,y] (xy), and split a into exactly k (1kn) subarrays in such a way that:

  • Each subarray is formed by several continuous elements of a, that is, it is equal to al,al+1,,ar for some l and r (1lrn).
  • Each element from a belongs to exactly one subarray.
  • In each subarray the number of elements inside the range [x,y] (inclusive) is strictly greater than the number of elements outside the range. An element with index i is inside the range [x,y] if and only if xaiy.

Print any solution that minimizes yx.

Input

The input consists of multiple test cases. The first line contains a single integer t (1t3104) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers n and k (1kn2105) — the length of the array a and the number of subarrays required in the partition.

The second line of each test case contains n integers a1,a2,,an (1ain) where ai is the i-th element of the array.

It is guaranteed that the sum of n over all test cases does not exceed 2105.

Output

For each test case, print k+1 lines.

In the first line, print x and y — the limits of the found range.

Then print k lines, the i-th should contain li and ri (1lirin) — the limits of the i-th subarray.

You can print the subarrays in any order.

Note

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

In the second test, it is possible to choose the range [2,2], and split the array in subarrays (1,3) and (4,4), in subarray (1,3) there are 2 elements inside the range (a2 and a3) and 1 element outside (a1), in subarray (4,4) there is only 1 element (a4), and it is inside the range.

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

Solution:

  1. // Never give up
  2. // Code by the Great Adarsh
  3. #include <bits/stdc++.h>
  4. using namespace std;
  5. #define ll long long int
  6. #define INF 1e9
  7. #define all(x) (x).begin(), (x).end()
  8. int main()
  9. {
  10. ios_base::sync_with_stdio(false);
  11. cin.tie(NULL);
  12.  
  13. int t;
  14. cin >> t;
  15. while (t--)
  16. {
  17. ll n;
  18. cin >> n;
  19. vector<ll> v(n+1);
  20. for (int i = 1; i <= n; i++)
  21. cin >> v[i];
  22. ll i = n - 1;
  23. ll ans = 0;
  24. while (i >0)
  25. {
  26. if (v[i] == v[n])
  27. i--;
  28. else
  29. {
  30. ans++;
  31. i -= (n - i);
  32. }
  33. }
  34. cout << ans << endl;
  35. }
  36. return 0;
  37. }

No comments:

Post a Comment