【牛客网】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的更多相关文章

  1. 牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献)

    牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献) 链接:https://ac.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy ha ...

  2. 2018牛客网暑期ACM多校训练营(第十场)J Rikka with Nickname(二分,字符串)

    链接:https://ac.nowcoder.com/acm/contest/148/J?&headNav=acm 来源:牛客网 Rikka with Nickname 时间限制:C/C++ ...

  3. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  4. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  5. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  6. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  7. Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  8. Longest Common Subsequence & Substring & prefix

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  9. 牛客网 --java问答题

    http://www.nowcoder.com/ 主要是自己什么都不怎么会.在这里可以学习很多的! 第一天看题自己回答,第二天看牛客网的答案! 1 什么是Java虚拟机?为什么Java被称作是“平台无 ...

随机推荐

  1. 二分图匹配——p3386 p2071 p2319 p1129(矩阵游戏)

    ---恢复内容开始--- 二分图,就是给你一个图,可以将点分为两部分,每一部分的点都能唯一映射到另一个集合里,也就是有连边: 注:以下转自 http://blog.csdn.net/dark_scop ...

  2. Alphat【翻译】

    翻译自:CFD-online 帖子地址:http://www.cfd-online.com/Forums/openfoam-solving/144625-alphat.html Wien3: 早上好 ...

  3. httpPostedFile实现WEBAPI文件上传

    public void PostUpload() { var httpPostedFile = HttpContext.Current.Request.Files; foreach(string p ...

  4. git如何获取获取子模块的代码?

    答: 步骤如下: 1. git submodule init 2. git submodule update

  5. linux内核中的__cpu_suspend是在哪里实现的呀?

    1. 内核版本 4.19 2. 在arch/arm/kernel/sleep.S中实现如下: /* * Save CPU state for a suspend. This saves the CPU ...

  6. 002-01-RestTemplate-配置使用说明

    一.概述 Spring RestTemplate 是 Spring 提供的用于访问 Rest 服务的客户端,RestTemplate 提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写 ...

  7. ARM程序的RO段、RW段和ZI段 --Image

    Limit 含义了解RO,RW和ZI需要首先了解以下知识:ARM程序的组成此处所说的“ARM程序”是指在ARM系统中正在执行的程序,而非保存在ROM中的bin映像(image)文件,这一点清注意区别. ...

  8. IfcSlab

    // IfcRoot ----------------------------------------------------------- // attributes: // shared_ptr& ...

  9. 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_17-课程预览功能开发-前后端测试

    启动前端代码 前端课程找到课程的发布页面 这样就打开了预览页面 结束

  10. 【420】链表实现Quack

    quack.h // quack.h: an interface definition for a queue/stack #include <stdio.h> #include < ...