-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path426-shrinking-array.cpp
More file actions
68 lines (65 loc) · 1.85 KB
/
Copy path426-shrinking-array.cpp
File metadata and controls
68 lines (65 loc) · 1.85 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Shrinking Array [2112B]
* Problem: https://codeforces.com/problemset/problem/2112/B
* Verdict: ACCEPTED Solved: 2025-08-31
* Language: C++23 (GCC 14-64, msys2)
* Runtime: 608 ms Memory: 0 KB
* Tags: brute force, greedy
* Author: BidoTeima
* Source: https://codeforces.com/contest/2112/submission/336324619
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int a[n];
for(auto&i:a)cin>>i;
int ans = -1;
for(int i = 0; i < n; i++){
int l=-1,g=-1;
for(int j = 0; j < i; j++){
if(a[j] < a[i])l=max(l,j);
if(a[j] > a[i])g=max(g,j);
}
if(l != -1 && g != -1){
if(ans == -1) ans = i - min(l,g) - 1;
else ans = min(ans, i - min(l,g) - 1);
}
for(int j = i + 1; j < n; j++){
if(abs(a[i]-a[j]) <= 1){
if(ans == -1) ans = j - i - 1;
else ans = min(ans, j - i - 1);
}
}
}
reverse(a,a+n);
for(int i = 0; i < n; i++){
int l=-1,g=-1;
for(int j = 0; j < i; j++){
if(a[j] < a[i])l=max(l,j);
if(a[j] > a[i])g=max(g,j);
}
if(l != -1 && g != -1){
if(ans == -1) ans = i - min(l,g) - 1;
else ans = min(ans, i - min(l,g) - 1);
}
for(int j = i + 1; j < n; j++){
if(abs(a[i]-a[j]) <= 1){
if(ans == -1) ans = j - i - 1;
else ans = min(ans, j - i - 1);
}
}
}
cout<<ans<<'\n';
}
return 0;
}