CodeVS 3289NOIP 2013花匠
Posted abclzr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeVS 3289NOIP 2013花匠相关的知识,希望对你有一定的参考价值。
http://codevs.cn/problem/3289/
dp转移,树状数组维护前缀max和后缀max进行优化,$O(nlogn)$。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 100003; int in() { int k = 0, fh = 1; char c = getchar(); for(; c < ‘0‘ || c > ‘9‘; c = getchar()) if (c == ‘-‘) fh = -1; for(; c >= ‘0‘ && c <= ‘9‘; c = getchar()) k = (k << 3) + (k << 1) + c - ‘0‘; return k * fh; } int n, h[N], H[N], cnt, bit1[N], bit2[N]; void update1(int pos, int x) { for(; pos; pos -= (pos & (-pos))) bit1[pos] = max(bit1[pos], x); } int Max1(int pos) { int ret = 0; for(; pos < cnt; pos += (pos & (-pos))) ret = max(ret, bit1[pos]); return ret; } void update2(int pos, int x) { for(; pos < cnt; pos += (pos & (-pos))) bit2[pos] = max(bit2[pos], x); } int Max2(int pos) { int ret = 0; for(; pos; pos -= (pos & (-pos))) ret = max(ret, bit2[pos]); return ret; } int ans = 0, f; int main() { n = in(); cnt = n; for(int i = 1; i <= n; ++i) { h[i] = in(); H[i] = h[i]; } sort(H + 1, H + n + 1); cnt = unique(H + 1, H + cnt + 1) - H; for(int i = 1; i <= n; ++i) h[i] = lower_bound(H + 1, H + cnt, h[i]) - H; update1(h[1], 1); update2(h[1], 1); for(int i = 2; i <= n; ++i) { f = Max1(h[i] + 1); update2(h[i], f + 1); ans = max(ans, f); f = Max2(h[i] - 1); update1(h[i], f + 1); ans = max(ans, f); } printf("%d\n", ans + 1); return 0; }
QwQ
以上是关于CodeVS 3289NOIP 2013花匠的主要内容,如果未能解决你的问题,请参考以下文章