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. Reverse Word in a String(翻转字符串)&字符串最后一个单词的长度

    1.题目: Given an input string, reverse the string word by word. For example,Given s = "the sky is ...

  2. ios tcpdump

    转载 前提条件:机器要破解,cydia能打开 需要工具1.openssh2.tcpdump 安装工具方法:1.连接网络,打开cydia2.确认Cydia设置为开发者模式(管理->设置->开 ...

  3. 【tomacat集群】Linux或 window配置多个Tomcat同时运行-完美解决-未来星开发团队-费元星

    Linux系统下怎样配置多个Tomcat同时运行呢,首先修改变量为第一个tomcat,然后修改第二个tomcat启动的脚本 如何在同一系统里同时启动多个Tomcat    http://www.cnb ...

  4. JavaSE总结--异常

    throwable Error: Exception: 编译型异常: 运行时异常:

  5. PowerShell脚本授权最佳实践

    [TechTarget中国原创] Windows PowerShell已成为微软在Windows Server上提供的首选管理界面.因为深度整合到Windows Server操作系统,PowerShe ...

  6. 容器技术的落地还要依靠SDN

    容器能够实现新应用程序的快速部署,代表着目前IT开发社区的最热门趋势之一.然而,想要实现容器部署生产环境,IT人员还需要使用SDN技术,在分布式微应用程序之间实现可扩展.可管理且安全的通信. 什么是容 ...

  7. USACO Section1.5 Prime Palindromes 解题报告

    pprime解题报告 —— icedream61 博客园(转载请注明出处)--------------------------------------------------------------- ...

  8. pythondifflib模块讲解示例

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Lockey23/article/details/77913855 difflib模块提供的类和方法用 ...

  9. freemaker示例

    第一步  创建一个User.java文件 来两个变量 public class User {      private String userName;       private String us ...

  10. python 学习分享-实战篇类 Fabric 主机管理程序开发

    # 类 Fabric 主机管理程序开发: # 1. 运行程序列出主机组或者主机列表 # 2. 选择指定主机或主机组 # 3. 选择让主机或者主机组执行命令或者向其传输文件(上传/下载) # 4. 充分 ...