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 3289】【NOIP 2013】花匠的更多相关文章

  1. NOIP 2013 花匠

    有多种方案,找拐点数目最简单O(n) 注意此题有相邻点价值一样,代码改变一点 #include <cstdio> #include<iostream> #include< ...

  2. NOIP 2013 花匠 神仙操作

    题目:https://www.luogu.org/problemnew/show/P1970 今天又学习了一个新的神仙操作: 标签是DP,想了一下,没什么心情写,默默打开题解——(狂喜!) 一位大佬( ...

  3. codevs 3289 花匠

    题目:codevs 3289 花匠 链接:http://codevs.cn/problem/3289/ 这道题有点像最长上升序列,但这里不是上升,是最长"波浪"子序列.用动态规划可 ...

  4. NOIP 2013 货车运输【Kruskal + 树链剖分 + 线段树 】【倍增】

    NOIP 2013 货车运输[树链剖分] 树链剖分 题目描述 Description A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在 ...

  5. Luogu 1979 NOIP 2013 华容道(搜索,最短路径)

    Luogu 1979 NOIP 2013 华容道(搜索,最短路径) Description 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面 ...

  6. [Noip 2013 Day1-3] 货车运输 做法总结

    [Noip 2013 Day1-3] 货车运输 做法总结 Online Judge:Luogu-1967 Label:启发式合并,离线,整体二分,按秩合并,倍增,最大生成树 打模拟离线赛时做到,顺便总 ...

  7. Codevs 3289 花匠 2013年NOIP全国联赛提高组

    3289 花匠 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 花匠栋栋种了一排花,每株花都 ...

  8. 【CodeVS 3290】【NOIP 2013】华容道

    http://codevs.cn/problem/3290/ 据说2013年的noip非常难,但Purpleslz学长还是AK了.能A掉这道题真心orz. 设状态$(i,j,k)$表示目标棋子在$(i ...

  9. NOIp 2013 #2 花匠 Label:爆0的Water

    题目描述 花匠栋栋种了一排花,每株花都有自己的高度.花儿越长越大,也越来越挤.栋栋决定 把这排中的一部分花移走,将剩下的留在原地,使得剩下的花能有空间长大,同时,栋栋希 望剩下的花排列得比较别致. 具 ...

随机推荐

  1. APDU

    # APDU # 定义:APDU(ApplicationProtocolDataUnit--应用协议数据单元).协议数据单元PDU(ProtocolDataUnit)是指对等层次之间传递的数据单位.协 ...

  2. 阿里云 CentOS6.5 ssh连接慢的解决方案

    我租了一台阿里云深圳的服务器,用的是CentOS6.5的系统,最近要在服务器上小改点代码,但是不管用private shell 还是securecrt工具连接,连上去后,都特别慢,经常敲一段代码要过个 ...

  3. c语言结构体小知识

    引自:http://c.biancheng.net/cpp/html/88.html 结构体在内存中是连续存储的 struct stu{ char *name; //姓名 int num; //学号 ...

  4. 编辑器插件数据保存之Serializable

    Editor数据保存需求 做编辑器插件开发时,当打开一个窗口,对数值进行修改后,在关闭窗口或重新打开Unity时,希望能保存上次的数据. 相关知识 Serialization ,ScriptableO ...

  5. 转: Git远程操作详解 (阮一峰)

    说明: 通过这个说明终于把远程操作给搞明白了. http://www.ruanyifeng.com/blog/2014/06/git_remote.html

  6. PHP核心技术与最佳实践--笔记

    <?php error_reporting(E_ALL); /* php 5.3引入 延迟静态绑定 */ /* php5.4引入trait,用来实现多层继承 trait Hello{} trai ...

  7. offline .net3.5

    1.加载虚拟光驱 2.dism.exe /online /enable-feature /featurename:netfx3 /Source:D:\sources\sxs

  8. 51nod lyk与gcd

    1678 lyk与gcd 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 这天,lyk又和gcd杠上了.它拥有一个n个数的数列,它想实现两种操作. 1:将  ai  ...

  9. Java 基础命名空间

    java.lang (提供利用 Java 编程语言进行程序设计的基础类)java.lang.annotation(提供了引用对象类,支持在某种程度上与垃圾回收器之间的交互)java.lang.inst ...

  10. office2016各个版本 以及 解决visio搜索任何都提示无匹配项问题

    http://tieba.baidu.com/p/4089747196 版本:Office 2016 Visio 专业版 32位版文件名:SW_DVD5_Visio_Pro_2016_W32_ChnS ...