#树状数组,离散#C 波动序列

分析
设\(dp[i][j][0/1/2/3]\)表示前\(i\)个位置当前选的数为\(j\),
且选择的是第一行/第二行/第三行不下降/第三行不上升,
状态转移方程显然,用线段树或者树状数组维护一下就可以了
代码
#include <cstdio>
#include <cctype>
#include <algorithm>
#define rr register
using namespace std;
const int N = 100011;
int n, m, a[3][N], b[N * 3];
inline signed iut() {
rr int ans = 0, f = 1;
rr char c = getchar();
while (!isdigit(c)) f = (c == '-') ? -f : f, c = getchar();
while (isdigit(c)) ans = (ans << 3) + (ans << 1) + (c ^ 48), c = getchar();
return ans * f;
}
inline signed max(int a, int b) { return a > b ? a : b; }
inline signed fan(int now) { return m - now + 1; }
struct Tree_Array {
int c[N * 3];
inline void update(int x, int y) {
for (; x <= m; x += -x & x) c[x] = max(c[x], y);
}
inline signed query(int x) {
rr int ans = 0;
for (; x; x -= -x & x) ans = max(ans, c[x]);
return ans;
}
} h[3][2];
signed main() {
freopen("sequence.in", "r", stdin);
freopen("sequence.out", "w", stdout);
n = iut();
for (rr int i = 1; i <= n; ++i) a[0][i] = iut(), b[++m] = a[0][i];
for (rr int i = 1; i <= n; ++i) a[1][i] = iut(), b[++m] = a[1][i];
for (rr int i = 1; i <= n; ++i) a[2][i] = iut(), b[++m] = a[2][i];
sort(b + 1, b + 1 + m), m = unique(b + 1, b + 1 + m) - b - 1;
for (rr int i = 1; i <= n; ++i)
a[0][i] = lower_bound(b + 1, b + 1 + m, a[0][i]) - b,
a[1][i] = lower_bound(b + 1, b + 1 + m, a[1][i]) - b,
a[2][i] = lower_bound(b + 1, b + 1 + m, a[2][i]) - b;
for (rr int i = 1; i <= n; ++i) {
rr int f1 = max(h[0][0].query(a[0][i]), max(h[1][0].query(a[0][i]), h[2][0].query(a[0][i]))) + 1;
rr int f2 =
max(h[0][1].query(fan(a[1][i])), max(h[1][1].query(fan(a[1][i])), h[2][1].query(fan(a[1][i])))) +
1;
rr int f3 = max(h[0][0].query(a[2][i]), h[1][0].query(a[2][i])) + 1,//一直不下降
f4 = max(h[0][1].query(fan(a[2][i])), h[2][1].query(fan(a[2][i]))) + 1;//一直不上升
h[0][0].update(a[0][i], f1), h[0][1].update(fan(a[0][i]), f1), h[0][0].update(a[1][i], f2),
h[0][1].update(fan(a[1][i]), f2), h[1][0].update(a[2][i], f3), h[1][1].update(fan(a[2][i]), f3),
h[2][0].update(a[2][i], f4), h[2][1].update(fan(a[2][i]), f4);
}
return !printf("%d", max(h[0][0].query(m), max(h[1][0].query(m), h[2][0].query(m))));
}
#树状数组,离散#C 波动序列的更多相关文章
- 【BZOJ】1818: [Cqoi2010]内部白点(树状数组+离散+特殊的技巧)
http://www.lydsy.com/JudgeOnline/problem.php?id=1818 这一题一开始我就看错了,bzoj的那个绝对值109简直坑人,应该是10^9,我直接写了个暴力. ...
- LightOJ 1085 - All Possible Increasing Subsequences 树状数组+离散
http://www.lightoj.com/volume_showproblem.php?problem=1085 题意:求一个序列的递增子序列个数. 思路:找规律可以发现,某个数作为末尾数的种类数 ...
- bzoj 5055: 膜法师 树状数组+离散
先枚举每一个数,看它前面有几个比它小,算一下和为sum1,后面有几个比它大,算一下和为sum2,对答案的贡献为A[i]*sum1*sum2. 离散化后,树状数组就可以了. 就是倒着一边,顺着一边,统计 ...
- HDU-4605 Magic Ball Game 树状数组+离散+dfs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4605 题意:给一颗树,每个节点有个权值w[u],每个节点只有两个儿子或者没有儿子,从根节点放下一个小球 ...
- hdu_5877_Weak Pair(离散+DFS+树状数组)
题目链接:hdu_5877_Weak Pair 题意: 给你一棵树,让你找有多少对满足那两个条件的weak pair 题解: 有人用Treap,我不会,然后我用树状数组+离散来替代Treap,用DFS ...
- codeforce div2 C 树状数组
http://codeforces.com/contest/362 题目大意:给你一个序列,用冒泡排序法让他变为非递减的序列最少需要几次.在冒泡交换之间,你有一个swap操作,该swap操作是交换任意 ...
- BZOJ1264 [AHOI2006]基因匹配Match 动态规划 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1264 题意概括 给出两个长度为5*n的序列,每个序列中,有1~n各5个. 求其最长公共子序列长度. ...
- Ultra-QuickSort---poj2299 (归并排序.逆序数.树状数组.离散化)
题目链接:http://poj.org/problem?id=2299 题意就是求把数组按从小到大的顺序排列,每次只能交换相邻的两个数, 求至少交换了几次 就是求逆序数 #include<std ...
- Codeforces - 828E DNA Evolution —— 很多棵树状数组
题目链接:http://codeforces.com/contest/828/problem/E E. DNA Evolution time limit per test 2 seconds memo ...
- ACM学习历程—HDU4417 Super Mario(树状数组 && 离线)
Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability re ...
随机推荐
- pikachu sql inject delete 注入
留言板输入几条信息 出现删除按钮,点他 通过burpsuite拦截请求,请求报文如下 GET /vul/sqli/sqli_del.php?id=57 HTTP/1.1 Host: 192.168.1 ...
- SpringBoot事务注解@Transactional 事物回滚、手动回滚事物
处理springboot 下提交事务异常,数据库没有回滚的问题. spring的文档中说道,spring声明式事务管理默认对非检查型异常和运行时异常进行事务回滚,而对检查型异常则不进行回滚操作. 什么 ...
- tempfile创建临时文件或目录
import tempfile tempfile.TemporaryFile() # 创建文件,返回文件对象 tempfile.NamedTemporaryFile() # 同上,不过会生成带有文件名 ...
- 【Azure Entra ID】如何在中国区获取用户 StrongAuthenticationUserDetails 和 StrongAuthenticationMethods 信息
问题描述 如何在中国区获取用户 StrongAuthenticationUserDetails 和 StrongAuthenticationMethods 信息 ? StrongAuthenticat ...
- 【Azure ACR+App Service】ACR WebHook请求App Service时遇见 401 Unauthorized
问题描述 App Service 支持从ACR中直接拉取镜像,并且可以配置持续部署(Continuous Deployment), 它是通过在ACR中添加一个Webhook,然后发送POST请求到 & ...
- 【Azure 环境】ARM部署模板大于4MB的解决方案及Linked Template遇见存储账号防火墙无法访问
问题一:在ADF Pipeline部署ARM Template报错"Deployment failed -- the request content size exceeds the max ...
- [Python] 子线程退出孙线程不退出
遇到了一个大坑! 如图,在子线程ThreadFunc退出之后,ThreadFunc2依旧在运行... 根本不会结束 但是官方文档中说明了,只要设置了daemon不为None 就能设置子线程是守护线程, ...
- 使用 Java 在Excel中创建下拉列表
下拉列表(下拉框)可以确保用户仅从预先给定的选项中进行选择,这样不仅能减少数据输入错误,还能节省时间提高效率.在MS Excel中,我们可以通过 "数据验证" 提供的选项来创建下拉 ...
- Codeforces Round 345 (Div. 1)A. Watchmen(容斥原理)
A. Watchmen 当欧几里得距离和曼哈顿距离相等时,\(x1==x2||y1==y2\) 这两个条件满足其一.这和容斥原理一样,至少选择一个的条件. 我们可以计算xi,以及小于i之前的这些,这样 ...
- 因IPv4和IPv6协议不同而引发的第三方接口调用失效的问题
记录一次因IPv4和IPv6协议不同而引发的第三方接口调用失效的问题,仅供大家参考!!! 背景介绍 公司有一个微信小程序,我做后端的,负责给小程序提供数据接口.后来因为一系列原因小程序要对接一个中控( ...