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. Android通过用代码画虚线椭圆边框背景来学习一下shape的用法

    在Android程序开发中,我们经常会去用到Shape这个东西去定义各种各样的形状,shape可以绘制矩形环形以及椭圆,所以只需要用椭圆即可,在使用的时候将控件比如imageview或textview ...

  2. runloop和线程有什么关系?

    每条线程都有唯一的一个RunLoop对象与之对应的 主线程的RunLoop是自动创建并启动 子线程的RunLoop需要手动启动 子线程的RunLoop创建步骤如下: 获得RunLoop对象后要调用ru ...

  3. Jenkins拾遗--第五篇-git插件填坑

    Jenkins使用过程中,大部分Job的第一项就行从源码库里签出代码.由于git越来越流行,所以,稍微新一些的项目的源码管理都是基于git的.对应的,jenkins的git plugin几乎是大部分j ...

  4. 解决ubuntu发热严重的问题

    对于双显卡PC安装ubuntu ,风扇狂转,发热严重,原因基本双显卡的优化导致. 解决具体步骤如下: 命令行输入sudo apt-get install bumblebee bumblebee-nvi ...

  5. QA面试题:之一(中英文题目、难度:简单)

    QA面试题:之一(中英文题目.难度:简单)

  6. jenkins构建定时任务

    jenkins构建定时任务的主要两种形式 1.Build periodically:周期性构建项目,无论有没有代码更新到时间都会构建项目 2.Poll SCM:定时检查源码变更(根据SCM软件的版本号 ...

  7. springboot07 mysql02

    多表关系 一.表关系介绍 1. 表之间为什么要有关系 一般来讲,通常都是一张表某一类型数据,比如学生数据存储在学生表,教师数据存储在教师表,学科数据存储在学科表.但是有时候我们需要表示一个学生属于哪一 ...

  8. solr集群搭建(复制)

    Solr集群的搭建以及使用(内涵zookeeper集群的搭建指南) 1   什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引 ...

  9. poj2388 更水

    Who's in the Middle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34974   Accepted: 2 ...

  10. LINQ to Entities 不识别方法“System.Guid Parse(System.String)”,因此该方法无法转换为存储表达式。

    LINQ to Entities 不识别方法"System.Guid Parse(System.String)",因此该方法无法转换为存储表达式. linq 中不能转换类型