-
Notifications
You must be signed in to change notification settings - Fork 126
/
UVa1618.cc
executable file
·85 lines (79 loc) · 2.1 KB
/
UVa1618.cc
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
// Weak Key, ACM/ICPC Seoul 2004, UVa1618
// 陈锋
#include <bits/stdc++.h>
#define _for(i, a, b) for (int i = (a); i < (b); ++i)
using namespace std;
const int maxk = 5000 + 4;
vector<int> H[maxk], L[maxk];
typedef vector<int>::iterator VIT;
int k, N[maxk];
// Nq > Ns > Np > Nr
// p
// q N[q] > N[p] q in H[p]
// r N[r] < N[p] r in L[p] && r > q
// s N[s] > N[p] && N[s] < N[q] && s > r, s in H[p] && s in L[q] && s > r
bool find1(int p) {
for (size_t qi = 0; qi < H[p].size(); qi++) { // Nq > Np, && q > p
int q = H[p][qi];
VIT rit = upper_bound(L[p].begin(), L[p].end(), q);
if (rit == L[p].end())
continue;
int r = *rit; // Nr < Nq, r > q
assert(r > q);
VIT sit = upper_bound(H[p].begin(), H[p].end(), r);
while (sit != H[p].end()) {
int s = *sit++;
assert(s > r);
if (binary_search(L[q].begin(), L[q].end(), s))
return true;
}
}
return false;
}
// Nq < Ns < Np < Nr
// p
// q q in L[p]
// r r > q && r in H[p]
// s s > r && s in H[q] && s in L[p]
bool find2(int p) { // Nq > Ns > Np > Nr p<q<r<s
for (size_t qi = 0; qi < L[p].size(); qi++) { // Nq > Np, && q > p
int q = L[p][qi];
VIT rit = upper_bound(H[p].begin(), H[p].end(), q);
if (rit == end(H[p]))
continue;
int r = *rit; // Nr < Nq, r > q
assert(r > q);
VIT sit = upper_bound(L[p].begin(), L[p].end(), r);
while (sit != L[p].end()) {
int s = *sit++;
assert(s > r);
if (binary_search(H[q].begin(), H[q].end(), s))
return true;
}
}
return false;
}
bool solve() {
_for(i, 0, k) if (find1(i) || find2(i)) return true;
return false;
}
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int T;
cin >> T;
while (T--) {
cin >> k;
_for(i, 0, k) cin >> N[i], H[i].clear(), L[i].clear();
_for(i, 0, k) {
_for(j, i + 1, k) {
if (N[j] > N[i])
H[i].push_back(j);
else if (N[j] < N[i])
L[i].push_back(j);
}
}
cout << (solve() ? "YES" : "NO") << endl;
}
return 0;
}
// 20497013 1618 Weak Key Accepted C++11 0.050 2017-12-16 11:45:09