[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, introduced in the preceding problem. He realizes that interaction between some pairs of breeds is actually acceptable 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 on each side), please help FJ determine the maximum number of crosswalks he can draw over his road, such 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^6
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
首先可以想到二维dp,设\(f[i][j]\)表示序列A的前\(i\)个数和序列B的前\(j\)个数的最大连边数,那么就有
\]
但是这样转移会TLE,于是我们需要给\(f[i][j]\)增加一个限制条件,即\(A_i\)与\(B_j\)之间有连线,这样的话转移即为\(f[i][j]=max(f[x][y]+1),(1<x<i,1<y<j)\),这样转移是需要前缀最大值的,于是就可以使用树状数组进行优化,而且第二维可以省去
/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
#define lowbit(x) ((x)&(-x))
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
int x=0,f=1;char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-'),x=-x;
if (x>9) print(x/10);
putchar(x%10+'0');
}
const int N=1e5;
int v[N+10],pos[N+10],now[N+10],tree[N+10],n;
void insert(int x,int v){for (;x<=n;x+=lowbit(x)) tree[x]=max(tree[x],v);}
int Query(int x){
if (!x) return 0;
int res=0;
for (;x;x-=lowbit(x)) res=max(res,tree[x]);
return res;
}
int main(){
n=read();
for (int i=1;i<=n;i++) v[i]=read();
for (int i=1;i<=n;i++) pos[read()]=i;
for (int i=1;i<=n;i++){
for (int j=max(1,v[i]-4);j<=min(n,v[i]+4);j++) now[j]=Query(pos[j]-1);
for (int j=max(1,v[i]-4);j<=min(n,v[i]+4);j++) insert(pos[j],now[j]+1);
}
printf("%d\n",Query(n));
return 0;
}
[Usaco2017 Feb]Why Did the Cow Cross the Road II (Platinum)的更多相关文章
- 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 ...
- [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 ...
- BZOJ4990 [Usaco2017 Feb]Why Did the Cow Cross the Road II 动态规划 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4990 题意概括 有上下两行长度为 n 的数字序列 A 和序列 B,都是 1 到 n 的排列,若 a ...
- BZOJ4993 [Usaco2017 Feb]Why Did the Cow Cross the Road II 动态规划 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4993 题意概括 有上下两行长度为 n 的数字序列 A 和序列 B,都是 1 到 n 的排列,若 a ...
- [BZOJ4993||4990] [Usaco2017 Feb]Why Did the Cow Cross the Road II(DP + 线段树)
传送门 f[i][j]表示当前第i个,且最后一个位置连接到j 第一维可以省去,能连边的点可以预处理出来,dp可以用线段树优化 #include <cstdio> #include < ...
- [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]间可以连一条边. 现要求在边与边不相交的情 ...
- [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 ...
- 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 ...
- [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: ...
随机推荐
- JUNIT -- springMVC的action进行单元测试
原文:http://blog.csdn.net/gaopeng0071/article/details/49946575 我开发环境springMVC版本3.0.4 样例代码: package com ...
- python读取大文件的方法
python计算文件的行数和读取某一行内容的实现方法 :最简单的办法是把文件读入一个大的列表中,然后统计列表的长度.如果文件的路径是以参数的形式filepath传递的,那么只用一行代码就可以完成我们的 ...
- memcached源代码分析-----set命令处理流程
转载请注明出处:http://blog.csdn.net/luotuo44/article/details/44236591 前一篇博文以get命令为样例把整个处理流程简单讲述了一遍.本篇博文将以se ...
- 使用CEF类库处理HTTP请求
当我们基于CEF开发应用时,可能会有URL请求处理的需求,比如HTTP下载或上传,此时可以利用CEF提供的类库来完成,而不必自己实现或引入其它第三方的类库. 在CEF里为URL Request设计了两 ...
- 【转载】同步和互斥的POSIX支持(互斥锁,条件变量,自旋锁)
上篇文章也蛮好,线程同步之条件变量与互斥锁的结合: http://www.cnblogs.com/charlesblc/p/6143397.html 现在有这篇文章: http://blog.cs ...
- java 返回json数据
Student st1 = new Student(1, "dg", 18, new Date()); Student st2 = new Student(2 ...
- iptables防火墙以及网络协议基本原理
一. Linux 网络安全模型 1. 防火墙: 工作在主机或者网络边缘,对进出报文使用实现定义的规则进行检测,并且由匹配的规则进行处理的一组硬件或者软件.也可能两者结合. 1) 通常使用的防火墙设备 ...
- 专题开发十二:JEECG微云高速开发平台-基础用户权限
专题开发十二:JEECG微云高速开发平台-基础用户权限 11.3.4自己定义button权限 Jeecg中.眼下button权限设置,是通过对平台自己封装的button标签(<t:dgFun ...
- C++实现KMP模式匹配算法
#include<iostream> #include<string> #include<vector> using namespace std; void Nex ...
- EJB学习笔记六(EJB中的拦截器)
1.前言 听到拦截器,预计都不陌生,尤其是在Servlet规范中,充分应用了拦截器的概念.EJB3也提供了拦截器的支持,本质上是轻量级的AOP实现.拦截器能够将多个业务方法中的通用逻辑从业务方法中抽 ...