-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2103c.cpp
More file actions
111 lines (90 loc) · 2.77 KB
/
Copy path2103c.cpp
File metadata and controls
111 lines (90 loc) · 2.77 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#pragma optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define all(x) begin(x), end(x)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
int main() {
cin.tie(0)->sync_with_stdio(0);
int t; cin >> t;
while (t--) {
int n, k; cin >> n >> k;
vi nums(n); // 1 if <= k, 0 otherwise
for (int i = 0; i < n; i++) {
cin >> nums[i];
nums[i] = ( nums[i] <= k ) ? 1 : -1;
}
vi psum(n + 1);
for (int i = 0; i < n; i++) {
psum[i + 1] = psum[i] + nums[i];
}
vi max_dp(n), min_dp(n);
max_dp[n-1] = psum[n-1];
min_dp[n-1] = psum[n-1];
for (int i = n-2; i > 0; i--) {
max_dp[i] = max(max_dp[i+1], psum[i]);
min_dp[i] = min(min_dp[i+1], psum[i]);
}
bool good = false;
for (int i = 1; i < n - 1; i++) {
int first = psum[i];
int second = max_dp[i+1] - psum[i];
int third = psum[n] - max_dp[i+1];
int count = (first >= 0) + (second >= 0) + (third >= 0);
if (count >= 2) {
good = true;
break;
}
second = min_dp[i+1] - psum[i];
third = psum[n] - min_dp[i+1];
count = (first >= 0) + (second >= 0) + (third >= 0);
if (count >= 2) {
good = true;
break;
}
}
// do the problem again but reverse the array
reverse(nums.begin(), nums.end());
psum.clear();
for (int i = 0; i < n; i++) {
psum[i + 1] = psum[i] + nums[i];
}
max_dp.clear();
min_dp.clear();
max_dp[n-1] = psum[n-1];
min_dp[n-1] = psum[n-1];
for (int i = n-2; i > 0; i--) {
max_dp[i] = max(max_dp[i+1], psum[i]);
min_dp[i] = min(min_dp[i+1], psum[i]);
}
for (int i = 1; i < n - 1; i++) {
int first = psum[i];
int second = max_dp[i+1] - psum[i];
int third = psum[n] - max_dp[i+1];
int count = (first >= 0) + (second >= 0) + (third >= 0);
if (count >= 2) {
good = true;
break;
}
second = min_dp[i+1] - psum[i];
third = psum[n] - min_dp[i+1];
count = (first >= 0) + (second >= 0) + (third >= 0);
if (count >= 2) {
good = true;
break;
}
}
if (good) {
cout << "YES" << endl;
} else {
cout << "NO" << endl;
}
}
}