-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday64.cpp
More file actions
51 lines (45 loc) · 1.07 KB
/
day64.cpp
File metadata and controls
51 lines (45 loc) · 1.07 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
#include <iostream>
#include <vector>
#include <stack>
#include <climits>
using namespace std;
bool hasRailwayDipPattern(const vector<int>& heights) {
int n = heights.size();
if (n < 3) return false;
vector<int> min_left(n);
min_left[0] = heights[0];
for (int i = 1; i < n; ++i) {
min_left[i] = min(min_left[i - 1], heights[i]);
}
stack<int> s;
for (int j = n - 1; j >= 0; --j) {
if (heights[j] > min_left[j]) {
while (!s.empty() && s.top() <= min_left[j]) {
s.pop();
}
if (!s.empty() && s.top() < heights[j]) {
return true;
}
s.push(heights[j]);
}
}
return false;
}
int main() {
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> heights(n);
for (int i = 0; i < n; ++i) {
cin >> heights[i];
}
if (hasRailwayDipPattern(heights)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
return 0;
}