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. Hbase物理存储

    物理模型 每个column family存储在HDFS上的一个单独文件中,空值不会被保存. Key 和 Version number在每个column family中均有一份: HBase为每个值维护 ...

  2. Java代码中获取配置文件(config.properties)中内容的两种方法

    方法千千万,本人暂时只总结了两种方法. (1)config.properties中的内容如图 在applicationContext.xml中配置 <!-- 引入配置文件 --> < ...

  3. laravel5.5http会话机制

    1.配置文件 config/session.php 大多数是用file驱动,将session保存在storage/framework/sessions,可以考虑使用redis或者memcached 驱 ...

  4. Android学习记录(9)—Android之Matrix的用法

    Matrix ,中文里叫矩阵,高等数学里有介绍,在图像处理方面,主要是用于平面的缩放.平移.旋转等操作. 首先介绍一下矩阵运算.加法和减法就不用说了,对应位相加就好.图像处理,主要用到的是乘法 .下面 ...

  5. USACO Section1.5 Superprime Rib 解题报告

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

  6. 17、bootStrap组件

    1.bootStrap组件 无数可复用的组件,包括字体图标.下拉菜单.导航.警告框.弹出框等更多功能. 2.字体图标 ①不要和其他图标混合使用 ②只能对内容为空的元素起作用 3.下拉菜单 <di ...

  7. Flask 教程精简版之一(系列片)

    Flask 教程精简版之一(系列片) 现在连教程都有精简版 准备 1.要学会 Flask 之前必须掌握 Python 基本使用. 2.会使用简单的 HTML 效果更加 3.若想练气功必须先自暴自弃 简 ...

  8. ASP.NET Core [1]:Hosting(笔记)

    参考:http://www.cnblogs.com/RainingNight/p/hosting-in-asp-net-core.html

  9. 【PTA】Tree Traversals Again

    题目如下: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For e ...

  10. lombok 去除麻烦的实体类get和set,toString书写

    首先在pom.xml中添加 <dependency> <groupId>org.projectlombok</groupId> <artifactId> ...