【牛客网】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被称作是“平台无 ...
随机推荐
- 认识wsgi
WSGI是什么? WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义 ...
- Postgresql修改字段的长度
alter table tbl_exam alter column question type character varing(1000); alter table tbl_exam alter c ...
- DELPHI搭建centos开发环境
DELPHI搭建centos7开发环境 关闭防火墙 搭建开发环境,还是直接关闭LINUX防火墙,省事. 否则,使用到的网络端口号,都要在防火墙开放,麻烦. systemctl disable fire ...
- arcgis python 使用光标和内存中的要素类将数据加载到要素集 学习:http://zhihu.esrichina.com.cn/article/634
学习:http://zhihu.esrichina.com.cn/article/634使用光标和内存中的要素类将数据加载到要素集 import arcpy arcpy.env.overwriteOu ...
- 8个华丽而实用的Java图表类库
8个华丽而实用的Java图表类库 转 https://www.300168.com/yidong/show-2744.html 核心提示:学习Java的同学注意了!!! 学习过程中遇到什么问题或者 ...
- ObjectAnimator简单示例
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- redis修改持久化路径、日志路径、清缓存
redis修改持久化路径和日志路径 vim redis.conf logfile /data/redis_cache/logs/redis.log #日志路径 dir /data/redis_cach ...
- SQL SERVER SELECT语句中加锁选项的详细说明
共享锁(读锁)和排他锁(写锁) 共享锁(S锁):共享 (S) 用于不更改或不更新数据的操作(只读操作),如 SELECT 语句. 如果事务T对数据A加上共享锁后,则其他事务只能对A再加共享锁,不能 ...
- VC3DGraphicsWindowQt
VC3DGraphicsWindowQt::VC3DGraphicsWindowQt(QWidget* parent, Qt::WindowFlags f) { osg::DisplaySetting ...
- 阶段5 3.微服务项目【学成在线】_day09 课程预览 Eureka Feign_10-课程详情页面静态化-课程详情模型数据查询接口
根据课程详情页面写一个获取数据模型的接口 目录的数据来自于课程计划表 右侧是课程的图片 需要写一个接口 获取课程相关的所有信息. 所以就需要一个模型类,里面包含了基本信息.图片信息.等各种详情页面的信 ...