传送门

f[i][j]表示当前第i个,且最后一个位置连接到j

第一维可以省去,能连边的点可以预处理出来,dp可以用线段树优化

#include <cstdio>
#include <iostream>
#include <algorithm>
#define N 100001
#define root 1, 1, n
#define ls now << 1, l, mid
#define rs now << 1 | 1, mid + 1, r using namespace std; int n, ans, cnt;
int a[N], pos[N], sum[N << 2], f[N]; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline int query(int now, int l, int r, int x, int y)
{
if(x > y) return 0;
if(x <= l && r <= y) return sum[now];
int mid = (l + r) >> 1, ret = 0;
if(x <= mid) ret = max(ret, query(ls, x, y));
if(mid < y) ret = max(ret, query(rs, x, y));
return ret;
} inline void update(int now, int l, int r, int x, int d)
{
if(l == r)
{
sum[now] = max(sum[now], d);
return;
}
int mid = (l + r) >> 1;
if(x <= mid) update(ls, x, d);
else update(rs, x, d);
sum[now] = max(sum[now << 1], sum[now << 1 | 1]);
} int main()
{
int i, j, x;
n = read();
for(i = 1; i <= n; i++) x = read(), pos[x] = i;
for(i = 1; i <= n; i++)
{
cnt = 0;
x = read();
for(j = max(1, x - 4); j <= min(n, x + 4); j++) a[++cnt] = pos[j];
sort(a + 1, a + cnt + 1);
for(j = cnt; j >= 1; j--)
{
x = query(root, 1, a[j] - 1) + 1;
f[a[j]] = max(f[a[j]], x);
update(root, a[j], x);
}
}
for(i = 1; i <= n; i++) ans = max(ans, f[i]);
printf("%d\n", ans);
return 0;
}

  

[BZOJ4993||4990] [Usaco2017 Feb]Why Did the Cow Cross the Road II(DP + 线段树)的更多相关文章

  1. 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 线段树维护dp

    题目 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 链接 http://www.lydsy.com/JudgeOnline/proble ...

  2. [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II dp

    4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II Time Limit: 10 Sec  Memory Limit: 128 MBSubmi ...

  3. BZOJ4993 [Usaco2017 Feb]Why Did the Cow Cross the Road II 动态规划 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4993 题意概括 有上下两行长度为 n 的数字序列 A 和序列 B,都是 1 到 n 的排列,若 a ...

  4. BZOJ4990 [Usaco2017 Feb]Why Did the Cow Cross the Road II 动态规划 树状数组

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4990 题意概括 有上下两行长度为 n 的数字序列 A 和序列 B,都是 1 到 n 的排列,若 a ...

  5. [Usaco2017 Feb]Why Did the Cow Cross the Road II (Platinum)

    Description Farmer John is continuing to ponder the issue of cows crossing the road through his farm ...

  6. [Usaco2017 Feb]Why Did the Cow Cross the Road II (Gold)

    Description 上下有两个长度为n.位置对应的序列A.B, 其中数的范围均为1~n.若abs(A[i]-B[j])<= 4,则A[i]与B[j]间可以连一条边. 现要求在边与边不相交的情 ...

  7. [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II

    Description Farmer John is continuing to ponder the issue of cows crossing the road through his farm ...

  8. 4989: [Usaco2017 Feb]Why Did the Cow Cross the Road

    题面:4989: [Usaco2017 Feb]Why Did the Cow Cross the Road 连接 http://www.lydsy.com/JudgeOnline/problem.p ...

  9. [BZOJ4989][Usaco2017 Feb]Why Did the Cow Cross the Road 树状数组维护逆序对

    4989: [Usaco2017 Feb]Why Did the Cow Cross the Road Time Limit: 10 Sec  Memory Limit: 256 MBSubmit:  ...

随机推荐

  1. 引用library之——带有自定义属性的自定义控件的library包

    一般来讲,当自定义一个控件Panel并且此控件有自定义属性时(例如:panel:closedHandle="@drawable/foot_bar_right"),xml中需要定义此 ...

  2. CodeForces 149D Coloring Brackets (区间DP)

    题意: 给一个合法的括号序列,仅含()这两种.现在要为每对括号中的其中一个括号上色,有两种可选:蓝or红.要求不能有两个同颜色的括号相邻,问有多少种染色的方法? 思路: 这题的模拟成分比较多吧?两种颜 ...

  3. (八)maven学习之继承

    继承 如果项目划分了多个模块,都需要依赖相似的jar包,只需要创建一个父模块,在它的pom.xml文件中配置依赖的jar包.功能模块只需要继承父模块,就可以自动得到其依赖的jar包,而不需要再每个模块 ...

  4. MIPS——循环语句

    有关指令 add $t1,$t2,$t3 #寄存器+寄存器,$t1 = $t2 + $t3 add $t1,$t2,immediate #寄存器+立即数,$t1 = $t2 + immediate b ...

  5. Linux平台搭建roboframework

    安装步骤介绍: . 在Centos7..1503下,默认的python的版本2./site-packages/). 2.安装pip 第一步: ()下载setuptools包 # wget http:/ ...

  6. python之set (集合)

    1. 集合是什么 set {1,2,3} 2. 集合怎么用 去重 集合是无序的 集合就是一个没有值的字典,遵循:唯一,无序,元素要求可哈希(不可变) 集合是可变的 2.1 增 方法一: s.updat ...

  7. ASIHTTPRequest简单学习

    ASIHTTPRequest框架是优秀的第三方Objective-C的HTTP框架,支持Mac OS X和iOS下的HTTP开发. 一.ASIHTTPRequest框架的安装和配置 (1)首先要在项目 ...

  8. html节点类型

    <li> nodeType:节点类型: <br> 1--->Element节点: <br> 3--->Text节点: <br> 8---&g ...

  9. vue新手入坑之mounted和created的区别(生命周期)

    这几个月用vue框架新做了一个项目,也算是边学习边实践吧.学习中也看过一些别人的开源项目,起初对mounted和created有一些疑惑,查询相关资料发现,这和vue的生命周期有关,在此也就做一个总结 ...

  10. Linux 中 MySQL 授权远程连接

    说明:当别的机子(IP )通过客户端的方式在没有授权的情况下是无法连接 MySQL 数据库的,如果需要远程连接 Linux 系统上的 MySQL 时,必须为其 IP 和具体用户进行授权.一般 root ...