[luoguP3402] 最长公共子序列(DP + 离散化 + 树状数组)
比 P1439 排列LCS问题,难那么一点点,只不过有的元素不是两个串都有,还有数据范围变大,树状数组得打离散化。
不过如果用栈+二分的话还是一样的。
——代码
#include <cstdio>
#include <algorithm> const int MAXN = ;
int n, m, size, ans;
int a[MAXN], b[MAXN], c[MAXN << ], q[MAXN << ], p[MAXN << ]; inline int max(int x, int y)
{
return x > y ? x : y;
} inline int query(int x)
{
int sum = ;
for(; x; x -= x & -x) sum = max(sum, c[x]);
return sum;
} inline void update(int x, int d)
{
for(; x <= size; x += x & -x) c[x] = max(c[x], d);
} int main()
{
int i, j, x;
scanf("%d %d", &n, &m);
for(i = ; i <= n; i++) scanf("%d", &a[i]), q[i] = a[i];
for(i = ; i <= m; i++) scanf("%d", &b[i]), q[i + n] = b[i];
std::sort(q + , q + n + m + );
size = std::unique(q + , q + n + m + ) - (q + );
for(i = ; i <= n; i++) a[i] = std::lower_bound(q + , q + size + , a[i]) - q;
for(i = ; i <= m; i++)
{
b[i] = std::lower_bound(q + , q + size + , b[i]) - q;
p[b[i]] = i;
}
for(i = ; i <= n; i++)
if(p[a[i]])
{
x = query(p[a[i]] - ) + ;
update(p[a[i]], x);
ans = max(ans, x);
}
printf("%d", ans);
return ;
}
[luoguP3402] 最长公共子序列(DP + 离散化 + 树状数组)的更多相关文章
- uestc oj 1217 The Battle of Chibi (dp + 离散化 + 树状数组)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 给你一个长为n的数组,问你有多少个长度严格为m的上升子序列. dp[i][j]表示以a[i]结尾长为j ...
- bzoj千题计划316:bzoj3173: [Tjoi2013]最长上升子序列(二分+树状数组)
https://www.lydsy.com/JudgeOnline/problem.php?id=3173 插入的数是以递增的顺序插入的 这说明如果倒过来考虑,那么从最后一个插入的开始删除,不会对以某 ...
- 洛谷-P1439 【模板】最长公共子序列 (DP,离散化)
题意:给两个长度为\(n\)的全排列,求他们的LCS 题解:这题给的数据范围到\(10^5\),用\(O(n^2)\)的LCS模板过不了,但由于给的是两个全排列,他们所含的元素都是一样的,所以,我们以 ...
- 题目1533:最长上升子序列 (nlogn | 树状数组)
题目1533:最长上升子序列 http://ac.jobdu.com/problem.php?pid=1533 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:857 解决:178 题目描 ...
- BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组
BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组 Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店.在这里,一盘盘寿司通过传送带依次呈现在小Z眼前.不同的寿 ...
- 【bzoj4627】[BeiJing2016]回转寿司 离散化+树状数组
题目描述 给出一个长度为n的序列,求所有元素的和在[L,R]范围内的连续子序列的个数. 输入 第一行包含三个整数N,L和R,分别表示寿司盘数,满意度的下限和上限. 第二行包含N个整数Ai,表示小Z对寿 ...
- CodeForces 540E - Infinite Inversions(离散化+树状数组)
花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...
- Ultra-QuickSort(归并排序+离散化树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 50517 Accepted: 18534 ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
随机推荐
- 题解报告:hdu 1541 Stars(经典BIT)
Problem Description Astronomers often examine star maps where stars are represented by points on a p ...
- Ubuntu Server 上安装pip后pip命令报错的解决办法
Installation Do I need to install pip? pip is already installed if you are using Python 2 >=2.7.9 ...
- 员工管理系统(集合与IO流的结合使用 beta3.0 BufferedReader / ObjectOutputStream)
Employee.java package cn.employee_io; public class Employee { private String empId; private String n ...
- 转-Mac下Apache Tomcat安装配置
ava Web如果稍微知道一点,一般对Tomcat都不会陌生,Apache是普通服务器,本身只支持html即普通网页,可以通过插件支持PHP,还可以与Tomcat连通(单向Apache连接Tomcat ...
- iOS9 关于明文HTTP报错的修复方法
报错:App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. ...
- Java8-Lomda表达式
Lomda表达式 /** * All rights Reserved, Designed By www.bingo.com * @Title TestLamda.java * @author yang ...
- [BZOJ1085][SCOI2005]骑士精神 搜索
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1085 大的思路是迭代加深搜索,我们加一个明显的剪枝,当棋盘中位置不对的骑士的数目加上已经走 ...
- 掌握Spark机器学习库-07.6-线性回归实现房价预测
数据集 house.csv 数据概览 代码 package org.apache.spark.examples.examplesforml import org.apache.spark.ml.fea ...
- 阿里云设置指定ip访问实例
添加安全组规则 添加允许访问的外网IP,优先级设置为1,并将所有ip设置为拒绝访问,优先级设置为2. 参考地址: https://help.aliyun.com/document_detail/254 ...
- win+r 快速命令
control keymgr.dll 打开凭据管理器 secpol.msc 本地安全策略 mstsc 远程 msconfig 启动选项 %temp% 临时文件夹 \\192.168 ...