Skip to content

Instantly share code, notes, and snippets.

@joonas-yoon
Created April 17, 2020 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joonas-yoon/9e0d2ac6e94104de603ba0d532f6d09e to your computer and use it in GitHub Desktop.
Save joonas-yoon/9e0d2ac6e94104de603ba0d532f6d09e to your computer and use it in GitHub Desktop.
BOJ 1539 - 이진 검색 트리
#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