4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 93  Solved: 64
[Submit][Status][Discuss]

Description

Farmer John is continuing to ponder the issue of cows crossing the road through his farm, introduced
 in the preceding problem. He realizes that interaction between some pairs of breeds is actually acc
eptable if the breeds are friendly, a property that turns out to be easily characterized in terms of
 breed ID: breeds aa and bb are friendly if |a-b|≤4, and unfriendly otherwise. It is ok for cows to
 wander into fields designated for other breeds, as long as they are friendly.Given the ordering of 
N fields on both sides of the road through FJ's farm (again, with exactly one field for each breed o
n each side), please help FJ determine the maximum number of crosswalks he can draw over his road, s
uch that no two intersect, and such that each crosswalk joins a pair of fields containing two breeds
 that are friendly. Each field can be accessible via at most one crosswalk (so crosswalks don't meet
 at their endpoints).
上下有两个长度为n、位置对应的序列A、B,
其中数的范围均为1~n。若abs(A[i]-B[j]) <= 4,
则A[i]与B[j]间可以连一条边。现要求在边与边不相交的情况下的最大的连边数量。
n <= 10^5。
 

Input

The first line of input contains N (1≤N≤100,0000). 
The next N lines describe the order, by breed ID, of fields on one side of the road; 
each breed ID is an integer in the range 1…N 
The last N lines describe the order, by breed ID, of the fields on the other side of the road. 
Each breed ID appears exactly once in each ordering.
注意:两个序列都是全排列
 

Output

Please output the maximum number of disjoint "friendly crosswalks" Farmer John can draw across the road.
 

Sample Input

6
1
2
3
4
5
6
6
5
4
3
2
1

Sample Output

5

HINT

 

Source

Platinum

把最长公共子序列转换成最长上升子序列。

对于每一个bi,我们找到他可以配对的ai的位置,从大到小放到数组c里,对c这个新序列跑一次最长上升子序列就是答案了 。

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#define LL long long
using namespace std;
int read() {
char ch=getchar();int x=,f=;
while(!isdigit(ch)){ch=getchar();}
while(isdigit(ch)){x=x*+ch-'';ch=getchar();}
return x;
}
int n;
int a[],b[];
int pos[];
int c[],cnt;
int ans=,d[];
int main() {
n=read();
for(int i=;i<=n;i++) {a[i]=read();pos[a[i]]=i;}
for(int i=;i<=n;i++) b[i]=read();
for(int i=;i<=n;i++) {
int s[]={},sum=;
for(int j=;j<=;j++) {
if(j==){s[++sum]=pos[b[i]];continue;}
if(b[i]-j>) s[++sum]=pos[b[i]-j];
if(b[i]+j<=n) s[++sum]=pos[b[i]+j];
}
sort(s+,s+sum+);
for(int j=sum;j>=;j--) c[++cnt]=s[j];
}
for(int i=;i<=cnt;i++) {
int now=lower_bound(d+,d+ans+,c[i])-d;
ans=max(ans,now);
d[now]=c[i];
}
printf("%d",ans);
}

[BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II dp的更多相关文章

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

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

  2. [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 ...

  3. 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 ...

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

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

  5. [BZOJ4993||4990] [Usaco2017 Feb]Why Did the Cow Cross the Road II(DP + 线段树)

    传送门 f[i][j]表示当前第i个,且最后一个位置连接到j 第一维可以省去,能连边的点可以预处理出来,dp可以用线段树优化 #include <cstdio> #include < ...

  6. [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 ...

  7. [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]间可以连一条边. 现要求在边与边不相交的情 ...

  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. Spring Boot :Request请求处理流程

    技术交流群:233513714

  2. javascript类式继承模式#3——借用和设置原型

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. iOS 中 AFNetworking HTTPS 的使用

    由于我们公司由HTTP转HTTPS,出现了一系列问题特此记录下. 一.HTTPS 二.App Transport Security 三.iOS 中用HTTPS 注意的问题 四.使用 AFNetwork ...

  4. 安装LoadRunner11报缺少vc2005_sp1_with_atl_fix_redist的错误

    找到安装程序自带的lrunner\Chs\prerequisites\vc2005_sp1_redist,双击运行vcredist_x86.exe,再重新安装LoadRunner即可成功. 参考资料: ...

  5. python3知识点之---------字符串的介绍

    1. 定义 其实字符串就是一系列字符,用引号括起来的就是字符串,其中的引号可以是单引号或者双引号. 比如 "This is a string"   'This is a strin ...

  6. tomcat启动后服务访问404

      .  解决办法: 在tomcat文件中有个work文件夹.其中,tomcat属于admin用户,work属于 admin用户 ,启动服务由admin用户启动. 但是发现work文件下的目录权限属于 ...

  7. struct&&class 空的大小

    #include using namespace std; class ClassA { }; class ClassB { private: int b; }; class ClassC : pub ...

  8. SVN基本介绍

    SVN是一种项目合作开发的软件,参与项目的人员可以在不同的地方实现文件和目录的超时空共享. 两个重要的概念: 1.配置库(Repository) SVN的核心是配置库,储存所有的数据,配置库按照文件树 ...

  9. jQuery静态分页功能

    分页功能在做项目的过程中是常常用到的,下面是我常用的一款分页效果: 1.分页的CSS样式(page.css) #setpage { margin: 15px auto; text-align: cen ...

  10. 根文件系统制作、NFS配置与安装及利用NFS挂载根文件系统

    最近打算从头开始制作根文件系统,下面是开发过程. 一.根文件系统的制作 0.FHS(Filesystem Hierarchy Standard)标准介绍 该标准规定了根目录下各个子目录的名称及其存放的 ...