Created
April 17, 2020 10:15
-
-
Save joonas-yoon/9e0d2ac6e94104de603ba0d532f6d09e to your computer and use it in GitHub Desktop.
BOJ 1539 - 이진 검색 트리
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
#define FOR(i, n) for(int i=0; i<(n); ++i) | |
typedef long long lld; | |
map<int, int> h1, h2; | |
int main() { | |
int n; | |
scanf("%d", &n); | |
lld ans = 0; | |
FOR(t, n) { | |
int x; | |
scanf("%d", &x); | |
auto l = h1.lower_bound(x); | |
auto r = h2.lower_bound(-x); | |
int m = 0; | |
if (l != h1.end()) m = max(m, l->second); | |
if (r != h2.end()) m = max(m, r->second); | |
h1[x] = h2[-x] = 1 + m; | |
ans += 1LL + m; | |
} | |
printf("%lld\n", ans); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment