[题目链接]

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. 关于srand()rand()的用法

    转自:http://baike.baidu.com/link?url=bhos65ZKp8lEq_6chSsmQv29jHrqjN_IFGVMNod6BuicQ-3oCP5VsEn3RBjXBPvA7 ...

  2. 字符串常量与const常量内存区(——选自陈皓的博客)

    1. 一个常见的考点: char* p = "test"; 那么理利用指针p来改变字符串test的内容都是错误的非法的. 例如: p[0] = 's'; strcpy(p, &qu ...

  3. Android Application基本组成部分

    Android Application基本组成部分 四个核心的组件 Activity活动,主要用于前台和用户交互,即UI,Activity只是加载一个View而并非一个UI对象 Service服务,主 ...

  4. ThinkPHP __construct和_initialize的使用

    ThinkPHP框架中的__construct和_initialize的使用 父类(PlatformController.class.php): class PlatformController ex ...

  5. Swift--错误集:couldn’t be opened because you don’t have permission to view it

    bug复现过程  把snapkit拉入代码中时,也把里面的info.plist文件拖到项目中,运行时,提示“couldn’t be opened because you don’t have perm ...

  6. PAT (Advanced Level) 1034. Head of a Gang (30)

    简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> # ...

  7. Nginx配置upstream实现负载均衡及keepalived实现nginx高可用

    (原文链接:http://www.studyshare.cn/blog-front//blog/details/1159/0 ) 一.准备工作 1.准备两个项目,发布到不同的服务器上,此处使用2个虚拟 ...

  8. 制作自己的网站第二步***在Linux上装上需要的软件以及部署项目配置**

    在购买自己的服务器后,如果想要把项目跑起来,就得安装一些必要的软件. 这里只说一些最基础最基本最不可或缺的几个.其他的可以根据自己的需要 安装使用. 首先,那就是配置jdk了,我们可以通过一些工具把下 ...

  9. CSS制作简单图标

    CSS制作图标包括知识点如border-width.border-style.border-color.border-radius.对元素的定位拼接.旋转等结合起来. 图标效果如下(拖动滑块可缩放图标 ...

  10. 报错:An error occurred at line: 22 in the generated java file The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory

    org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 22 in ...