Range and Partition
Given an array
- Each subarray is formed by several continuous elements of
a , that is, it is equal toal,al+1,…,ar for somel andr (1≤l≤r≤n ). - 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 indexi is inside the range[x,y] if and only ifx≤ai≤y .
Print any solution that minimizes
The input consists of multiple test cases. The first line contains a single integer
The first line of each test case contains two integers
The second line of each test case contains
It is guaranteed that the sum of
For each test case, print
In the first line, print
Then print
You can print the subarrays in any order.
In the first test, there should be only one subarray, which must be equal to the whole array. There are
In the second test, it is possible to choose the range
In the third test, it is possible to choose the range
Solution:
- // Never give up
- // Code by the Great Adarsh
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long int
- #define INF 1e9
- #define all(x) (x).begin(), (x).end()
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- int t;
- cin >> t;
- while (t--)
- {
- ll n;
- cin >> n;
- vector<ll> v(n+1);
- for (int i = 1; i <= n; i++)
- cin >> v[i];
- ll i = n - 1;
- ll ans = 0;
- while (i >0)
- {
- if (v[i] == v[n])
- i--;
- else
- {
- ans++;
- i -= (n - i);
- }
- }
- cout << ans << endl;
- }
- return 0;
- }
No comments:
Post a Comment