-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupDown.cpp
More file actions
69 lines (57 loc) · 1.28 KB
/
upDown.cpp
File metadata and controls
69 lines (57 loc) · 1.28 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
69
#include <iostream>
#include <vector>
using namespace std;
int n;
int arr[100002];
int dp[100002][3];
int dpp(int i, int st) {
if (i > n) {
return 0;
}
if (dp[i][st] != -1) {
return dp[i][st];
}
if (i == n) {
dp[i][st] = 1;
}
else if (st == 1 && arr[i] < arr[i+1]) {
dp[i][st] = 1;
}
else if (st == 2 && arr[i] > arr[i+1]) {
dp[i][st] = 1;
}
else if (st == 1 && arr[i] >= arr[i+1]) {
dp[i][st] = 1 + dpp(i+1, 2);
}
else if (st == 2 && arr[i] <= arr[i+1]) {
dp[i][st] = 1 + dpp(i+1, 1);
}
return dp[i][st];
}
int main ()
{
int t;
cin >> t;
while (t--) {
cin >> n;
int arr[n];
for (int i = 1; i <= n; i++) cin >> arr[i];
memset(dp, -1, sizeof(dp));
for (int i = 1; i <= n; i++) {
dpp(i, 1);
dpp(i, 2);
}
int fin = -1;
for (int i = 1; i <= n; i++) {
int x = dp[i][2];
if (i+x > n) {
fin = max(fin, dp[i][2]+1);
} else if (i%2 == 0) {
fin = max(fin, dp[i][2] + 1 + dp[i+x][1]);
} else {
fin = max(fin, dp[i][2] + 1 + dp[i+x][2]);
}
}
cout << fin << "\n";
}
}