[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 ...
随机推荐
- 转 UTL_FILE Throws ORA-29284 Or ORA-29283 When Attempting To READ File
APPLIES TO: PL/SQL - Version 9.2.0.8 and laterInformation in this document applies to any platform.* ...
- $bzoj1009-HNOI2008$ $GT$考试 字符串$dp$ 矩阵快速幂
题面描述 阿申准备报名参加\(GT\)考试,准考证号为\(N\)位数\(x_1,x_2,...,x_n\ (0\leq x_i\leq 9)\),他不希望准考证号上出现不吉利的数字. 他的不吉利数字\ ...
- Java中多线程并发体系知识点汇总
一.多线程 1.操作系统有两个容易混淆的概念,进程和线程. 进程:一个计算机程序的运行实例,包含了需要执行的指令:有自己的独立地址空间,包含程序内容和数据:不同进程的地址空间是互相隔离的:进程拥有各种 ...
- (转)Python之文件读写
Python之文件读写 原文:https://www.cnblogs.com/huilixieqi/p/6494891.html 本节内容: I/O操作概述 文件读写实现原理与操作步骤 文件打开模式 ...
- 数据挖掘:提取百度知道QA中的影视信息
1. 背景 网站上爬取了部分关于影视的百度知道QA,为了后续提高影视的搜索效果,需要基于百度知道QA的答案抽取相关的影视信息. 2. 流程 目前已有基础的媒资视频库信息,基于媒资视频库中的视频名称,构 ...
- Nginx+Keepalived配置
1. Nginx安装 (1) 环境:分别在2台服务器上部署nginx且步骤一致: 如192.138.86.1和192.138.86.2 (2) 下载官网最新稳定版,地址:https://nginx.o ...
- What is Network Address Translation?---reference
http://whatismyipaddress.com/nat What is Network Address Translation? Network Address Translation (N ...
- 如何给swing加上alt+x和ctrl+x快捷键
1.给菜单栏上的菜单alt+x快捷键非常简单: private JMenu helpInfo = new JMenu("帮助"); helpInfo.setMnemonic(Key ...
- python之迭代器
原文 我们已经知道,可以直接作用于for循环的数据类型有以下几种: 一类是集合数据类型,如list.tuple.dict.set.str等: 一类是generator,包括生成器和带yield的gen ...
- drupal 基础理论
第3章 Drupal 的基本概念 添加新评论 浏览 6795 次 Drupal的基本概念主要包括节点.内容类型.模块.主题和分类等.只有对这些概念有了足够的了解,方能灵活的构建网站.本章将对这些基本概 ...