-
Notifications
You must be signed in to change notification settings - Fork 0
/
C. Make Equal Again.cpp
56 lines (40 loc) · 1.4 KB
/
C. Make Equal Again.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin >> t;
while (t--)
{
ll n;
cin >> n;
vector<ll> arr(n);
ll ans = INT_MAX;
for (ll i = 0; i < n; i++)
{
cin >> arr[i];
}
ll left = 0;
ll leftCount = 0;
ll right = n - 1;
ll rightCount = 0;
while (left < n and arr[left] == arr[0])
left++, leftCount++;
while (right >= 0 && arr[right] == arr[n - 1])
right--, rightCount++;
if (arr[0] == arr[n - 1])
cout << max(0ll, n - (leftCount + rightCount)) << "\n";
else
cout << min(n - leftCount, n - rightCount) << "\n";
}
}
/*
count Arr[0] equal element to
Our goal is to choose the shortest possible segment, which means to exclude as many elements as possible from the beginning and
end of the segment. Note that we can exclude only equal elements, and then assign a value equal to the excluded elements on the
segment. Let's find the lengths of the maximum prefix and suffix consisting of equal elements. Let's denote by k the number of
excluded elements, by k1 - the length of the maximum suitable prefix, by k2 - the maximum suitable suffix. Then if a[0] = a[n-1],
then k = k1 + k2 (exclude both prefix and suffix), otherwise k = max(k1, k2) (exclude the longer one).
The answer is n - k - all non-excluded elements must be replaced so that they become equal to the excluded one.
*/