[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=4990

[算法]

首先记录b中每个数的出现位置 , 记为P

对于每个ai , 枚举(ai - 4) - (ai + 4) , 将Pj从大到小加入序列

然后求最长上升子序列即可 , 详见代码

时间复杂度 : O(NlogN)

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXN 1000010 int n , len;
int a[MAXN] , pos[MAXN] , tmp[MAXN] , f[MAXN] , value[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x , y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x , y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
struct BinaryIndexedTree
{
int c[MAXN];
inline int lowbit(int x)
{
return x & (-x);
}
inline int query(int x)
{
int ret = ;
for (int i = x; i; i -= lowbit(i)) chkmax(ret , c[i]);
return ret;
}
inline void modify(int x , int val)
{
int ret = ;
for (int i = x; i <= n; i += lowbit(i)) chkmax(c[i] , val);
}
} BIT; int main()
{ read(n);
for (int i = ; i <= n; i++) read(a[i]);
for (int i = ; i <= n; i++)
{
int x;
read(x);
pos[x] = i;
}
for (int i = ; i <= n; i++)
{
int l = ;
for (int j = max(a[i] - , ); j <= min(a[i] + , n); j++) tmp[++l] = pos[j];
sort(tmp + ,tmp + l + ,greater<int>());
for (int j = ; j <= l; j++) value[++len] = tmp[j];
}
int ans = ;
for (int i = ; i <= len; i++)
{
f[i] = BIT.query(value[i] - ) + ;
BIT.modify(value[i] , f[i]);
chkmax(ans , f[i]);
}
printf("%d\n",ans); return ;
}

[USACO17FEB]Why Did the Cow Cross the Road II的更多相关文章

  1. 洛谷 P3662 [USACO17FEB]Why Did the Cow Cross the Road II S

    P3662 [USACO17FEB]Why Did the Cow Cross the Road II S 题目描述 The long road through Farmer John's farm ...

  2. [USACO17FEB]Why Did the Cow Cross the Road II S

    题目描述 The long road through Farmer John's farm has  crosswalks across it, conveniently numbered  (). ...

  3. 洛谷 P3657 [USACO17FEB]Why Did the Cow Cross the Road II P

    题面 大意:让你把两个n的排列做匹配,连线不想交,而且匹配的数字的差<=4,求最大匹配数 sol:(参考了kczno1的题解)对于第一个排列从左往右枚举,用树状数组维护到达另一个序列第i个数字的 ...

  4. [USACO17FEB]Why Did the Cow Cross the Road II P

    嘟嘟嘟 考虑dp. 对于ai,和他能匹配的bj只有9个,所以我们考虑从这9个状态转移. 对于ai 能匹配的一个bj,当前最大的匹配数一定是[1, j - 1]中的最大匹配数 + 1.然后用树状数组维护 ...

  5. BZOJ 4990 [USACO17FEB] Why Did the Cow Cross the Road II P (树状数组优化DP)

    题目大意:给你两个序列,你可以两个序列的点之间连边 要求:1.只能在点权差值不大于4的点之间连边 2.边和边不能相交 3.每个点只能连一次 设表示第一个序列进行到 i,第二个序列进行到 j,最多连的边 ...

  6. 题解【洛谷P3662】[USACO17FEB]Why Did the Cow Cross the Road II S

    本题是练习前缀和的好题!我们可以枚举前端点,确定一个长度为k的区间,然后利用前缀和统计区间内损坏的灯的数量,最后取最小值即可.AC代码: #include <bits/stdc++.h> ...

  7. 洛谷 P3663 [USACO17FEB]Why Did the Cow Cross the Road III S

    P3663 [USACO17FEB]Why Did the Cow Cross the Road III S 题目描述 Why did the cow cross the road? Well, on ...

  8. [USACO17FEB]Why Did the Cow Cross the Road III P

    [USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...

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

随机推荐

  1. hdu 1528 二分匹配

    #include<stdio.h> #include<string.h> int map[100][100],mark[100],link[100],max2,k; int f ...

  2. HH的项链(codevs 2307)

    题目描述 Description HH有一串由各种漂亮的贝壳组成的项链.HH相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH不断地收集新的贝壳,因此,他的 ...

  3. 【HDOJ6322】Euler Function(数论)

    题意: 思路: #include <stdio.h> #include <vector> #include <algorithm> #include <str ...

  4. 【POJ3294】Life Forms(后缀数组,二分)

    题意: n<=100 len[i]<=1000 思路:这是一道论文题 ..]of longint; ch:..]of ansistring; n,n1,l,r,mid,last,i,j,m ...

  5. php统计图类库JpGraph

    php统计图类库JpGraph JpGraph官网地址:https://jpgraph.net/. (1)下载类库: 下载地址:https://jpgraph.net/download/. 选择版本, ...

  6. linux日志服务器审计客户端history记录

    https://blog.csdn.net/yanggd1987/article/details/70255179

  7. OO第三单元总结——JML

    目录 写在前面 JML理论基础 JML工具链 JMLUnitNG的使用 架构设计 Bug分析 心得体会 写在前面 OO的第三单元学习结束了,本单元我们学习了如何使用JML语言来对我们的程序进行规格化设 ...

  8. java集合框架 hashMap 简单使用

    参考文章:http://blog.csdn.net/itm_hadf/article/details/7497462 通常,默认加载因子 (.75) 在时间和空间成本上寻求一种折衷.      加载因 ...

  9. ios UITableView 获取点击cell对象

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UITabl ...

  10. iOS macOS的后渗透利用工具:EggShell

    EggShell是一款基于Python编写的iOS和macOS的后渗透利用工具.它有点类似于metasploit,我们可以用它来创建payload建立侦听.此外,在反弹回的session会话也为我们提 ...