[题目链接]

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. [NOIP1999] 提高组 洛谷P1014 Cantor表

    题目描述 现代数学的著名证明之一是Georg Cantor证明了有理数是可枚举的.他是用下面这一张表来证明这一命题的: 1/1 1/2 1/3 1/4 1/5 … 2/1 2/2 2/3 2/4 … ...

  2. memcached 笔记之windows 7 下面 安装memcached 报错

    windows 7 下面 安装memcached 报错 两种情况: 一:服务确实已经安装过 .如需要重新安装,当然是先memcached.exe -d uninstall 二:奇怪的是服务确实没有安装 ...

  3. Animation显示ListView的每一条记录

    activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  4. CodeForces 597A Divisibility

    水题. #include<iostream> #include<cstring> #include<cmath> #include<queue> #in ...

  5. Date日期模式

    package cn.zmh.Date; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo ...

  6. eclipse菜单字体乱码的解决

    方法一: 这个跟活动控制台代码页有关. 如果要更改为 UTF-8,则需要运行 chcp 命令: chcp 65001 有时新安装的系统可能在运行一些中文软件时显示错乱,可通过控制面板修改系统区域来管理 ...

  7. 检查nginx配置,重载配置以及重启的方法

    原文  http://blogread.cn/it/article/4549?f=hot1 几个常用的nginx命令 Nginx 安装后只有一个程序文件,本身并不提供各种管理程序,它是使用参数和系统信 ...

  8. Nginx: 解决connect() to xxxx failed (13: Permission denied) while connecting to upstream的问题

    一句话:setsebool httpd_can_network_connect true

  9. django 简易博客开发 5 markdown支持、代码高亮、gravatar头像服务

    上一篇博客介绍了comments库使用及ajax支持,现在blog已经具备了基本的功能,但是只能发表文字,不支持富文本编辑.今天我们利用markdown添加富文本支持. markdown语法说明: h ...

  10. Linux监測某一时刻对外的IP连接情况

    相信大家都熟悉netstat命令吧,这里就主要採用此命令.网上流传的DDoS Deflate工具就是採用IP数量来统计对外连接数,然后结合Iptables的方法来实现某个IP增加黑名单和解禁某IP n ...