BZOJ1264 [AHOI2006]基因匹配Match 【LCS转LIS】
题目链接
题解
平凡的\(LCS\)是\(O(n^2)\)的
显然我们要根据题目的性质用一些不平凡的\(LCS\)求法
这就很巧妙了,,
我们考虑\(A\)序列的每个位置可能匹配\(B\)位置的哪些位置
而\(A\)序列中匹配的位置一定是单调递增的
那么我们就把\(A\)的每个位置所能匹配\(B\)的位置找出来,降序排列替代\(A\)原来的位置
我们就能得到一个新的序列,显然原序列的\(LCS\)就是新序列的\(LIS\)
而由于题目的限制,新序列只能使原序列长度的\(5\)倍
而求\(LIS\)是\(O(nlogn)\)的
就可以\(A\)了
比如
A:1 1 1 2 2 2 1 2 2 1
B:1 1 1 1 1 2 2 2 2 2
1对应5 4 3 2 1
2对应10 9 8 7 6
那么新序列 5 4 3 2 1,5 4 3 2 1,5 4 3 2 1,10 9 8 7 6,10 9 8 7 6,10 9 8 7 6,.......
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#include<vector>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 500005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
vector<int> pos[maxn];
int n,A[maxn],B[maxn],C[maxn],bac[maxn],f[maxn],N,M;
int cal(int x){
int l = 0,r = N,mid;
while (l < r){
mid = l + r + 1 >> 1;
if (bac[mid] < x) l = mid;
else r = mid - 1;
}
return l;
}
int main(){
n = read(); N = n * 5;
REP(i,N) A[i] = read();
REP(i,N) B[i] = read(),pos[B[i]].push_back(i);
REP(i,N){
for (unsigned int j = pos[A[i]].size() - 1; ~j; j--)
C[++M] = pos[A[i]][j];
}
memset(bac,0x3f3f3f3f,sizeof(bac)); bac[0] = 0;
int ans = 0;
REP(i,M){
f[i] = cal(C[i]) + 1;
bac[f[i]] = min(bac[f[i]],C[i]);
ans = max(ans,f[i]);
}
printf("%d\n",ans);
return 0;
}
BZOJ1264 [AHOI2006]基因匹配Match 【LCS转LIS】的更多相关文章
- bzoj1264 [AHOI2006]基因匹配Match 树状数组+lcs
1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1255 Solved: 835[Submit][ ...
- BZOJ1264: [AHOI2006]基因匹配Match
1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 541 Solved: 347[Submit][S ...
- BZOJ 1264: [AHOI2006]基因匹配Match( LCS )
序列最大长度2w * 5 = 10w, O(n²)的LCS会T.. LCS 只有当a[i] == b[j]时, 才能更新答案, 我们可以记录n个数在第一个序列中出现的5个位置, 然后从左往右扫第二个序 ...
- BZOJ1264 [AHOI2006]基因匹配Match 动态规划 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1264 题意概括 给出两个长度为5*n的序列,每个序列中,有1~n各5个. 求其最长公共子序列长度. ...
- [BZOJ1264][AHOI2006]基因匹配Match(DP + 树状数组)
传送门 有点类似LCS,可以把 a[i] 在 b 串中的位置用一个链式前向星串起来,由于链式前向星是从后往前遍历,所以可以直接搞. 状态转移方程 f[i] = max(f[j]) + 1 ( 1 &l ...
- BZOJ1264——[AHOI2006]基因匹配Match
1.题意,求最长公共子序列,每个数字在序列中都出现5次 2.分析:最长公共子序列的标准解法是dp,$O(n^2)$过不了的,然后我们发现判断哪两个位置优化的地方用$5n$就可以搞定了,那么我们用BIT ...
- 【BZOJ1264】[AHOI2006]基因匹配Match DP+树状数组
[BZOJ1264][AHOI2006]基因匹配Match Description 基因匹配(match) 卡卡昨天晚上做梦梦见他和可可来到了另外一个星球,这个星球上生物的DNA序列由无数种碱基排列而 ...
- bzoj 1264: [AHOI2006]基因匹配Match
1264: [AHOI2006]基因匹配Match Description 基因匹配(match) 卡卡昨天晚上做梦梦见他和可可来到了另外一个星球,这个星球上生物的DNA序列由无数种碱基排列而成(地球 ...
- 1264: [AHOI2006]基因匹配Match
1264: [AHOI2006]基因匹配Match Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 982 Solved: 635[Submit][S ...
随机推荐
- logstash处理@timestamp时区
input { stdin { } } filter { #ruby { # code => "event.set('timestamp', event.get('@timestamp ...
- 使用Python的Requests库进行web接口测试
1.Requests简介Requests 是使用 Apache2 Licensed 许可证的 HTTP 库.用 Python 编写,真正的为人类着想. Python 标准库中的 urllib2 模块提 ...
- 11-Dockerfile构建镜像
用 Dockerfile 创建上节的 ubuntu-with-vi,其内容则为: FROM ubuntu RUN apt-get update && apt-get install v ...
- Spark任务执行期间写临时文件报错导致失败
spark任务在执行期间,有时候会遇到临时目录创建失败,导致任务执行错误. java.io.IOException: Failed to create local dir in -- spark执行过 ...
- openstack golang sdk使用
1. go get github.com/gophercloud/gophercloud import ( "github.com/gophercloud/gophercloud" ...
- 多重共性和VIF检验
图片来源https://wenku.baidu.com/view/7008df8383d049649b66581a.html 和 https://wenku.baidu.com/view/6acdf9 ...
- Gradle快速上手——从Maven到Gradle
[本文写作于2018年7月5日] 本文适合于有一定Maven应用基础,想快速上手Gradle的读者. 背景 Maven.Gradle都是著名的依赖管理及自动构建工具.提到依赖管理与自动构建,其重要性在 ...
- 梯度下降算法以及其Python实现
一.梯度下降算法理论知识 我们给出一组房子面积,卧室数目以及对应房价数据,如何从数据中找到房价y与面积x1和卧室数目x2的关系? 为了实现监督学习,我们选择采用自变量x1.x2的线性函数来评估因变 ...
- win2008 r2 开启TLS1.2
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityPr ...
- 解决Ubuntu16.04下git clone太慢问题
记录一些博客,省着自己再去找了... ss-qt5安装 生成.pac genpac --pac-proxy "SOCKS5 127.0.0.1:1080" --gfwlist-pr ...