[USACO17FEB]Why Did the Cow Cross the Road II P
考虑dp。
对于ai,和他能匹配的bj只有9个,所以我们考虑从这9个状态转移。
对于ai 能匹配的一个bj,当前最大的匹配数一定是[1, j - 1]中的最大匹配数 + 1。然后用树状数组维护前缀匹配数最大值就行了。
- #include<cstdio>
- #include<iostream>
- #include<cmath>
- #include<algorithm>
- #include<cstring>
- #include<cstdlib>
- #include<cctype>
- #include<vector>
- #include<stack>
- #include<queue>
- using namespace std;
- #define enter puts("")
- #define space putchar(' ')
- #define Mem(a, x) memset(a, x, sizeof(a))
- #define rg register
- typedef long long ll;
- typedef double db;
- const int INF = 0x3f3f3f3f;
- const db eps = 1e-;
- const int maxn = 1e5 + ;
- inline ll read()
- {
- ll ans = ;
- char ch = getchar(), last = ' ';
- while(!isdigit(ch)) {last = ch; ch = getchar();}
- while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
- if(last == '-') ans = -ans;
- return ans;
- }
- inline void write(ll x)
- {
- if(x < ) x = -x, putchar('-');
- if(x >= ) write(x / );
- putchar(x % + '');
- }
- int n, a[maxn], pos[maxn];
- int Max[maxn];
- int c[maxn];
- int lowbit(int x)
- {
- return x & -x;
- }
- void add(int pos, int x)
- {
- for(; pos <= n && c[pos] < x; pos += lowbit(pos)) c[pos] = x;
- }
- int query(int pos)
- {
- int ret = ;
- for(; pos; pos -= lowbit(pos)) if(c[pos] > ret) ret = c[pos];
- return ret;
- }
- int main()
- {
- n = read();
- for(int i = ; i <= n; ++i) a[i] = read();
- for(int i = ; i <= n; ++i) {int x = read(); pos[x] = i;}
- for(int i = ; i <= n; ++i)
- {
- for(int j = max(, a[i] - ); j <= min(n, a[i] + ); ++j)
- Max[j] = query(pos[j] - );
- for(int j = max(, a[i] - ); j <= min(n, a[i] + ); ++j)
- add(pos[j], Max[j] + );
- }
- write(query(n)), enter;
- return ;
- }
[USACO17FEB]Why Did the Cow Cross the Road II P的更多相关文章
- 洛谷 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 ...
- [USACO17FEB]Why Did the Cow Cross the Road II S
题目描述 The long road through Farmer John's farm has crosswalks across it, conveniently numbered (). ...
- 洛谷 P3657 [USACO17FEB]Why Did the Cow Cross the Road II P
题面 大意:让你把两个n的排列做匹配,连线不想交,而且匹配的数字的差<=4,求最大匹配数 sol:(参考了kczno1的题解)对于第一个排列从左往右枚举,用树状数组维护到达另一个序列第i个数字的 ...
- [USACO17FEB]Why Did the Cow Cross the Road II
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4990 [算法] 首先记录b中每个数的出现位置 , 记为P 对于每个ai , 枚举(a ...
- BZOJ 4990 [USACO17FEB] Why Did the Cow Cross the Road II P (树状数组优化DP)
题目大意:给你两个序列,你可以两个序列的点之间连边 要求:1.只能在点权差值不大于4的点之间连边 2.边和边不能相交 3.每个点只能连一次 设表示第一个序列进行到 i,第二个序列进行到 j,最多连的边 ...
- 题解【洛谷P3662】[USACO17FEB]Why Did the Cow Cross the Road II S
本题是练习前缀和的好题!我们可以枚举前端点,确定一个长度为k的区间,然后利用前缀和统计区间内损坏的灯的数量,最后取最小值即可.AC代码: #include <bits/stdc++.h> ...
- 洛谷 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 ...
- [USACO17FEB]Why Did the Cow Cross the Road III P
[USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...
- 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 ...
随机推荐
- html自定义垂直导航菜单(多级导航菜单,去掉font-awesome图标,添加自己的箭头图标)
这次在原先html自定义垂直导航菜单的基础上做了比较大的改动: 1.去掉了font-awesome图标,上级菜单右边的箭头是自己用css写的,具体参考<css三角箭头>. 2.去掉了初始化 ...
- spring利用cors处理跨域问题
参考 http://blog.csdn.net/isea533/article/details/50449907 写的很全面 http://blog.csdn.net/a317560315/artic ...
- elastic 集群安装
Elastic Search 安装和配置 1.下载 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6. ...
- (转)linux sed命令就是这么简单
sed替换命令 原文:https://www.cnblogs.com/zd520pyx1314/p/6061337.html http://www.cnblogs.com/wangqiguo/p/67 ...
- mysql存储过程嵌套循环并分页处理数据
业务背景:公司存证产品升级,随着数据量的增加,存证产品线按业务分表,导致以往的存证关联数据需要做数据同步更新.版本发布前,通过当前存储过程解决数据升级问题. ##创建存证文档关联情况下更新所用存储过程 ...
- 自定义django admin及其界面
1.在项目目录下新创建一个app,命名为kingadmin,在templates目录下新建kingadmin目录,用来存放相关页面的模板文件,新建一个templatetags目录,用来存放处理前端模板 ...
- 项目视图 Project Browser
项目视图 在这个视图,你可以访问.管理你当前项目资源. 项目视图左侧面板显示项目的文件夹结构的分层列表,当从列表中单击一个文件夹,其内容会显示在面板的右边.你可以点击小三角展开或折叠文件夹,显示他包含 ...
- Android活动的启动模式
1. standard 标准模式,是活动默认的启动模式,在不进行显示指定的情况下,所有活动都会自动使用这种模式. Android使用返回栈管理活动,在standard模式下,每当启动一个新的活动,它就 ...
- Cocos2d-x手游技术分享(1)-【天天打蚊子】数据存储与音效篇
前言: 手游项目<天天打蚊子>终于上线,特地写几篇技术分享文章,分享一下其中使用到的技术,其中使用cocos2d-x引擎,首选平台iOS,也请有iPhone或者iPad的朋友帮忙下载好评. ...
- js数组插入指定位置元素,删除指定位置元素,查找指定位置元素算法
将元素x插入到顺序表L(数组)的第i个数据元素之前 function InsertSeqlist(L, x, i) { // 将元素x插入到顺序表L的第i个数据元素之前 if(L.length == ...