【牛客网】Longest Common Subsequence
【牛客网】Longest Common Subsequence
发现只有d数组最格路
于是我们把前三个数组中相同的数记成一个三维坐标,同一个数坐标不会超过8个
从前往后枚举d,每次最多只会更新不超过8个点
而每个点更新就是找这个点三维偏序都小于它的最大的一个值+1来更新它
用KD树来维护,这个点与树中节点三维的某一维不相交就退出
可以加的剪枝是如果树中最大值+1小于当前搜到的答案就退出
然后把新的值在树中进行更新
跑的还是挺快的,0.5s不到
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 10005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int N,dimension;
int a[MAXN],b[MAXN],c[MAXN],d[MAXN],rt,Ncnt;
vector<int> v[3][MAXN];
struct node {
int x,y,z;
friend bool operator < (const node &a,const node &b) {
if(dimension == 0) return a.x < b.x;
else if(dimension == 1) return a.y < b.y;
else return a.z < b.z;
}
friend bool operator == (const node &a,const node &b) {
return a.x == b.x && a.y == b.y && a.z == b.z;
}
friend void upmin(node &a,node b) {
a.x = min(a.x,b.x);a.y = min(a.y,b.y);a.z = min(a.z,b.z);
}
friend void upmax(node &a,node b) {
a.x = max(a.x,b.x);a.y = max(a.y,b.y);a.z = max(a.z,b.z);
}
};
struct KD_node {
node p;int v,maxv;
node av,bv;
int lc,rc;
}tr[MAXN * 8];
vector<node> pos[MAXN],line;
vector<int> rec[MAXN];
bool vis[15];
#define lc(u) tr[u].lc
#define rc(u) tr[u].rc
void build(int &u,int l,int r,int d) {
if(l > r) return;
u = ++Ncnt;
dimension = d;
int mid = (l + r) >> 1;
nth_element(line.begin() + l,line.begin() + mid,line.begin() + r + 1);
tr[u].p = line[mid];tr[u].v = 0;tr[u].maxv = 0;
tr[u].av = line[mid];tr[u].bv = line[mid];
build(lc(u),l,mid - 1,(d + 1) % 3);
build(rc(u),mid + 1,r,(d + 1) % 3);
upmin(tr[u].av,tr[lc(u)].av);upmin(tr[u].av,tr[rc(u)].av);
upmax(tr[u].bv,tr[lc(u)].bv);upmax(tr[u].bv,tr[rc(u)].bv);
}
bool sless(node a,node b) {
return a.x < b.x && a.y < b.y && a.z < b.z;
}
bool uless(node a,node b) {
return a.x <= b.x && a.y <= b.y && a.z <= b.z;
}
bool checkin(node x,int u) {
return uless(tr[u].av,x) && uless(x,tr[u].bv);
}
void update(int u) {
tr[u].maxv = max(tr[u].v,max(tr[lc(u)].maxv,tr[rc(u)].maxv));
}
void Change(int u,node p,int v) {
if(!u) return;
if(tr[u].p == p) {tr[u].v = v;update(u);return;}
if(!checkin(p,u)) return;
Change(lc(u),p,v);
Change(rc(u),p,v);
update(u);
}
int ta;
void Query(int u,node p) {
if(tr[u].maxv + 1 <= ta) return;
if(!sless(tr[u].av,p)) return;
if(sless(tr[u].p,p)) ta = max(ta,tr[u].v + 1);
Query(lc(u),p);Query(rc(u),p);
}
void Update_array(node p,int v) {
ta = max(v,1);
Query(1,p);
}
void Solve() {
read(N);
for(int i = 1 ; i <= N ; ++i) {read(a[i]);v[0][a[i]].pb(i);}
for(int i = 1 ; i <= N ; ++i) {read(b[i]);v[1][b[i]].pb(i);}
for(int i = 1 ; i <= N ; ++i) {read(c[i]);v[2][c[i]].pb(i);}
for(int i = 1 ; i <= N ; ++i) read(d[i]);
for(int i = 1 ; i <= N ; ++i) {
for(auto t0 : v[0][i]) {
for(auto t1 : v[1][i]) {
for(auto t2 : v[2][i]) {
pos[i].pb((node){t0,t1,t2});rec[i].pb(0);
line.pb((node){t0,t1,t2});
}
}
}
}
tr[0].av = (node){0x7fffffff,0x7fffffff,0x7fffffff};tr[0].bv = (node){-1,-1,-1};
build(rt,0,line.size() - 1,0);
int ans = 0;
for(int i = 1 ; i <= N ; ++i) {
memset(vis,0,sizeof(vis));
for(int j = 0 ; j < pos[d[i]].size() ; ++j) {
Update_array(pos[d[i]][j],rec[d[i]][j]);
if(ta > rec[d[i]][j]) vis[j] = 1;
rec[d[i]][j] = ta;
ans = max(ans,ta);
}
for(int j = 0 ; j < pos[d[i]].size() ; ++j) {
if(vis[j]) Change(1,pos[d[i]][j],rec[d[i]][j]);
}
}
out(ans);enter;
}
int main(){
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Solve();
return 0;
}
【牛客网】Longest Common Subsequence的更多相关文章
- 牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献)
牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献) 链接:https://ac.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy ha ...
- 2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)
链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm 来源:牛客网 Rikka with Nickname 时间限制:C/C++ ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- Longest Common Subsequence & Substring & prefix
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- 牛客网 --java问答题
http://www.nowcoder.com/ 主要是自己什么都不怎么会.在这里可以学习很多的! 第一天看题自己回答,第二天看牛客网的答案! 1 什么是Java虚拟机?为什么Java被称作是“平台无 ...
随机推荐
- 实验1C语言开发环境使用和数据类型、运算符、表达式
# include <stdio.h> int main() { char ch; printf("输入一个字符:\n"); scanf("%c", ...
- js new call apply bind 的 原理
new new 做了什么事?1. 以 Object.protoype 为原型创建一个新对象 2. 以新对象为 this,执行函数的 [[call]] 3. 如果 [[call]] 的返回值是对象,那么 ...
- [vsftpd] ubuntu14.04 ansible剧本安装vsftpd流程及报错排查
需求: 在ubuntu14.04机器上搭建ftp服务,ftp账号通过winscp软件登录后,仅可增删改/data/wwwroot目录. 一.安装步骤 1.apt 安装vsftpd apt-get in ...
- docker笔记--容器之间如何互相免密?
在使用docker搭建hadoop分布式集群的时候,需要各容器之间相互免密登录,传统的方式我想或许会很麻烦,特别是当容器达到几百上千台的时候,这时就需要有一种方式来更简单实现免密登录了. 环境介绍: ...
- Python 学习随笔 - 1 - 基础数据类型、变量 及 基本运算
仅有的C语言的基础都是大学时学的: 准备赶潮流,开始学习Python. 随笔记录学习过程中,靠一点点C语言基础难以去理解的地方,以及区别于C语言的地方,做些笔记作为以后参考. Python 解释器直接 ...
- 2018-2019-2 20165312《网络攻防技术》Exp7 网络欺诈防范
2018-2019-2 20165312<网络攻防技术>Exp7 网络欺诈防范 目录 一.相关知识点总结 二.实验内容 三.实验步骤 四.实验总结及问题回答 五.实验中遇到的问题及解决方法 ...
- DELPHI正则表达式
DELPHI正则表达式 1)下载源码 官方网站: http://www.regular-expressions.info/delphi.html 直接下载: http://www.regula ...
- linux redis 设置密码:
在服务器上,这里以linux服务器为例,为redis配置密码. 1.第一种方式 (当前这种linux配置redis密码的方法是一种临时的,如果redis重启之后密码就会失效,) (1)首先进入redi ...
- Python中的logging模块就这么用
Python中的logging模块就这么用 1.日志日志一共分成5个等级,从低到高分别是:DEBUG INFO WARNING ERROR CRITICALDEBUG:详细的信息,通常只出现在诊断问题 ...
- 解决 MYSQL CPU 占用 100% 的经验总结
朋友主机(Windows 2003 + IIS + PHP + MYSQL )近来 MySQL 服务进程 (mysqld-nt.exe) CPU 占用率总为 100% 高居不下.此主机有10个左右的 ...