【CodeVS 3289】【NOIP 2013】花匠
2016-08-28 08:01
253 查看
http://codevs.cn/problem/3289/
dp转移,树状数组维护前缀max和后缀max进行优化,$O(nlogn)$。
QwQ
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 , H , cnt, bit1 , bit2 ; 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 3289】[NOIP 2013 day2 T2] 花匠(dp)
- 【Codevs】3289 花匠 --2013年NOIP全国联赛提高组 贪心
- Codevs 3289 花匠 2013年NOIP全国联赛提高组
- NOIP2013 花匠解题报告
- [NOIP2013]花匠
- codevs 3290 noip 2013 Day2 T3华容道
- NOIP 2013 D2T2 花匠
- NOIP2013花匠
- 【NOIP】2013 花匠
- Noip2013花匠题解
- 洛谷 1970 [NOIP2013] 花匠 贪心
- 【NOIP2013】洛谷1970 花匠
- noip2013 花匠
- 【NOIP2013花匠】(最长波动序列)
- 【NOIP】2013提高组 花匠(摆花)
- 刷过一题之NOIP2013花匠
- 洛谷P1970 [NOIP2013提高组Day2T2] 花匠
- 【NOIP2013提高组T5】花匠-O(n)横扫做法
- [noip2013]花匠
- 2013 NOIP提高组 花匠