嘟嘟嘟

考虑dp。

对于ai,和他能匹配的bj只有9个,所以我们考虑从这9个状态转移。

对于ai 能匹配的一个bj,当前最大的匹配数一定是[1, j - 1]中的最大匹配数 + 1。然后用树状数组维护前缀匹配数最大值就行了。

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<cmath>
  4. #include<algorithm>
  5. #include<cstring>
  6. #include<cstdlib>
  7. #include<cctype>
  8. #include<vector>
  9. #include<stack>
  10. #include<queue>
  11. using namespace std;
  12. #define enter puts("")
  13. #define space putchar(' ')
  14. #define Mem(a, x) memset(a, x, sizeof(a))
  15. #define rg register
  16. typedef long long ll;
  17. typedef double db;
  18. const int INF = 0x3f3f3f3f;
  19. const db eps = 1e-;
  20. const int maxn = 1e5 + ;
  21. inline ll read()
  22. {
  23. ll ans = ;
  24. char ch = getchar(), last = ' ';
  25. while(!isdigit(ch)) {last = ch; ch = getchar();}
  26. while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
  27. if(last == '-') ans = -ans;
  28. return ans;
  29. }
  30. inline void write(ll x)
  31. {
  32. if(x < ) x = -x, putchar('-');
  33. if(x >= ) write(x / );
  34. putchar(x % + '');
  35. }
  36.  
  37. int n, a[maxn], pos[maxn];
  38. int Max[maxn];
  39.  
  40. int c[maxn];
  41. int lowbit(int x)
  42. {
  43. return x & -x;
  44. }
  45. void add(int pos, int x)
  46. {
  47. for(; pos <= n && c[pos] < x; pos += lowbit(pos)) c[pos] = x;
  48. }
  49. int query(int pos)
  50. {
  51. int ret = ;
  52. for(; pos; pos -= lowbit(pos)) if(c[pos] > ret) ret = c[pos];
  53. return ret;
  54. }
  55.  
  56. int main()
  57. {
  58. n = read();
  59. for(int i = ; i <= n; ++i) a[i] = read();
  60. for(int i = ; i <= n; ++i) {int x = read(); pos[x] = i;}
  61. for(int i = ; i <= n; ++i)
  62. {
  63. for(int j = max(, a[i] - ); j <= min(n, a[i] + ); ++j)
  64. Max[j] = query(pos[j] - );
  65. for(int j = max(, a[i] - ); j <= min(n, a[i] + ); ++j)
  66. add(pos[j], Max[j] + );
  67. }
  68. write(query(n)), enter;
  69. return ;
  70. }

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

  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

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4990 [算法] 首先记录b中每个数的出现位置 , 记为P 对于每个ai , 枚举(a ...

  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. html自定义垂直导航菜单(多级导航菜单,去掉font-awesome图标,添加自己的箭头图标)

    这次在原先html自定义垂直导航菜单的基础上做了比较大的改动: 1.去掉了font-awesome图标,上级菜单右边的箭头是自己用css写的,具体参考<css三角箭头>. 2.去掉了初始化 ...

  2. spring利用cors处理跨域问题

    参考 http://blog.csdn.net/isea533/article/details/50449907 写的很全面 http://blog.csdn.net/a317560315/artic ...

  3. elastic 集群安装

    Elastic Search 安装和配置 1.下载 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6. ...

  4. (转)linux sed命令就是这么简单

    sed替换命令 原文:https://www.cnblogs.com/zd520pyx1314/p/6061337.html http://www.cnblogs.com/wangqiguo/p/67 ...

  5. mysql存储过程嵌套循环并分页处理数据

    业务背景:公司存证产品升级,随着数据量的增加,存证产品线按业务分表,导致以往的存证关联数据需要做数据同步更新.版本发布前,通过当前存储过程解决数据升级问题. ##创建存证文档关联情况下更新所用存储过程 ...

  6. 自定义django admin及其界面

    1.在项目目录下新创建一个app,命名为kingadmin,在templates目录下新建kingadmin目录,用来存放相关页面的模板文件,新建一个templatetags目录,用来存放处理前端模板 ...

  7. 项目视图 Project Browser

    项目视图 在这个视图,你可以访问.管理你当前项目资源. 项目视图左侧面板显示项目的文件夹结构的分层列表,当从列表中单击一个文件夹,其内容会显示在面板的右边.你可以点击小三角展开或折叠文件夹,显示他包含 ...

  8. Android活动的启动模式

    1. standard 标准模式,是活动默认的启动模式,在不进行显示指定的情况下,所有活动都会自动使用这种模式. Android使用返回栈管理活动,在standard模式下,每当启动一个新的活动,它就 ...

  9. Cocos2d-x手游技术分享(1)-【天天打蚊子】数据存储与音效篇

    前言: 手游项目<天天打蚊子>终于上线,特地写几篇技术分享文章,分享一下其中使用到的技术,其中使用cocos2d-x引擎,首选平台iOS,也请有iPhone或者iPad的朋友帮忙下载好评. ...

  10. js数组插入指定位置元素,删除指定位置元素,查找指定位置元素算法

    将元素x插入到顺序表L(数组)的第i个数据元素之前 function InsertSeqlist(L, x, i) { // 将元素x插入到顺序表L的第i个数据元素之前 if(L.length == ...