【牛客网】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被称作是“平台无 ...
随机推荐
- 【概率论】5-6:正态分布(The Normal Distributions Part II)
title: [概率论]5-6:正态分布(The Normal Distributions Part II) categories: - Mathematic - Probability keywor ...
- dashucoding记录2019.6.8
WordPress网站 网址: https://cn.wordpress.org/ 阿里云市场 https://market.aliyun.com/products/53616009?spm=a2c4 ...
- Jetbrain全栈最新激活方法(2019年及之前所有新老版本)
随着2019版的到来,之前的永久激活教程也不生效了,所以今天为大家带来一种新的永久激活方式. 1.下载新版破解补丁 破解补丁传送门提取码:3e8j 点击传送门下载补丁文件 jetbrains-agen ...
- luogu_P3674 小清新人渣的本愿
传送门 Solution 莫队,用bitset来存储出现的数 如果是和或者差,直接通过左移右移就可以实现判断 对于积的询问,暴力判就行了,因数只要枚举\(\sqrt n\)个 总复杂度是\(O(n^2 ...
- [WEB安全]代码/命令执行总结
0x01 代码执行 1.1 概念 远程代码执行实际上是调用服务器网站代码进行执行. 1.2 常见执行方法 eval eval():将字符串当做函数进行执行(需要传入一个完整的语句) demo: < ...
- nginx做反向代理时出现302错误
现象:nginx在使用非80端口做反向代理时,浏览器访问发现返回302错误 详细现象如下: 浏览器请求登录页: 输入账号密码点击登录: 很明显登录后跳转的地址少了端口号. 原因:proxy.conf文 ...
- Vue路由管理之Vue-router
一.Vue Router介绍 Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌.包含的功能有: 嵌套的路由/视图表 模块化的. ...
- Java 多线程编程(锁优化)
转:https://mp.weixin.qq.com/s/lDuguEhuWiLY8ofBRy3tZA 并发环境下进行编程时,需要使用锁机制来同步多线程间的操作,保证共享资源的互斥访问. 加锁会带来性 ...
- Linux如何使用shell命令检测PHP木马防止脚本木马的破坏
1.一句话查找PHP木马 代码如下 # find / -name "*.php" |xargs egrep "phpspy|c99sh|milw0rm|eval\(gun ...
- Oracle scope中 spfile、memory、both 的区别
Oracle里面有个叫做spfile的东西,就是动态参数文件,里面设置了Oracle 的各种参数. 所谓的动态,就是说你可以在不关闭数据库的情况下,更改数据库参数,记录在spfile里面. 更改参数的 ...