-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathB.cpp
More file actions
41 lines (38 loc) · 942 Bytes
/
B.cpp
File metadata and controls
41 lines (38 loc) · 942 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int a[100010];
int helper(int left, int right) {
if (left > right)
return 0;
while (left < right && a[left] == 1)
++left;
while (left < right && a[right] == 1)
++right;
int res = (*min_element(a + left, a + right + 1)) - 1;
for (int i = left; i <= right; ++i)
a[i] -= res;
for (int i = left; i <= right; ++i)
if (a[i] == 1) {
res += helper(left, i - 1);
left = i + 1;
}
res += helper(left, right);
return res;
}
int main() {
int t, n;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
int l = 0, r = n - 1;
while (l <= r && a[l] == 1)
++l;
while (l <= r && a[r] == 1)
--r;
int res = helper(l, r);
printf("%d\n", res);
}
return 0;
}