[Usaco2017 Feb]Why Did the Cow Cross the Road II (Gold)
Description
上下有两个长度为n、位置对应的序列A、B,
其中数的范围均为1~n。若abs(A[i]-B[j])<= 4,则A[i]与B[j]间可以连一条边。
现要求在边与边不相交的情况下的最大的连边数量。
n <= 10^3
Sample Input
6
1
2
3
4
5
6
6
5
4
3
2
1
Sample Output
5
网上有题解说求最长公共上升序列,一脸懵逼,反正我只会DP。设f[i][j]表示A序列选到第i个,B序列选到第j个的最大连线数,转移就十分明显了
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
}
const int N=1e3;
int f[N+10][N+10],A[N+10],B[N+10];
int main(){
int n=read();
for (int i=1;i<=n;i++) A[i]=read();
for (int i=1;i<=n;i++) B[i]=read();
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++){
f[i][j]=max(f[i-1][j],f[i][j-1]); //要么不选
if (abs(A[i]-B[j])<=4) f[i][j]=max(f[i][j],f[i-1][j-1]+1); //可以的话就选
}
printf("%d\n",f[n][n]);
return 0;
}
[Usaco2017 Feb]Why Did the Cow Cross the Road II (Gold)的更多相关文章
- 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 ...
- [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II dp
4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II Time Limit: 10 Sec Memory Limit: 128 MBSubmi ...
- BZOJ4990 [Usaco2017 Feb]Why Did the Cow Cross the Road II 动态规划 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4990 题意概括 有上下两行长度为 n 的数字序列 A 和序列 B,都是 1 到 n 的排列,若 a ...
- BZOJ4993 [Usaco2017 Feb]Why Did the Cow Cross the Road II 动态规划 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4993 题意概括 有上下两行长度为 n 的数字序列 A 和序列 B,都是 1 到 n 的排列,若 a ...
- [BZOJ4993||4990] [Usaco2017 Feb]Why Did the Cow Cross the Road II(DP + 线段树)
传送门 f[i][j]表示当前第i个,且最后一个位置连接到j 第一维可以省去,能连边的点可以预处理出来,dp可以用线段树优化 #include <cstdio> #include < ...
- [Usaco2017 Feb]Why Did the Cow Cross the Road II (Platinum)
Description Farmer John is continuing to ponder the issue of cows crossing the road through his farm ...
- [Usaco2017 Feb]Why Did the Cow Cross the Road III (Gold)
Description 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai < aj < bi < bj的对数 Sample Input ...
- [Usaco2017 Feb]Why Did the Cow Cross the Road I (Gold)
Description 有一幅n*n的方格图,n <=100,每个点上有一个值. 从(1,1)出发,走到(n,n),只能走上下左右. 每走一步花费t,每走三步需要花费走完三步后到达格子的值. 求 ...
- [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II
Description Farmer John is continuing to ponder the issue of cows crossing the road through his farm ...
随机推荐
- 基于sentry的前端错误监控日志系统(部署sentry服务器/前端项目部署)-让前端最快的定位到生产问题
背景 在这越来越发达的网络时代,web应用也是越来越复杂,尤其是前端的开发,也是越来越受重视. 所以在我们前端开发完成后,会有一些列的web应用的上线验证,如自测.QA测试.code review 等 ...
- 对CSS尺寸单位'em'的长期误解
一直以来认为'em'是相对于父元素的字体大小. 直到今天学习移动WEB开发,重新复习css的尺寸大小时,惊奇发现:对em深深的误解了!!! 在CSS官网对em的解释实例是: a. h1{line-he ...
- How can we listen for errors that do not trigger window.onerror?
原文: http://stackoverflow.com/questions/19141195/how-can-we-listen-for-errors-that-do-not-trigger-win ...
- [RxJS] Implement the `map` Operator from Scratch in RxJS
While it's great to use the RxJS built-in operators, it's also important to realize you now have the ...
- Deepin-还原Windows平台
首次启动! 是不是感觉很迷茫呢? 找不到存在感 先设置成Windows那种高校模式(右键下面任意区域) OK了吧,然后我们找到“启动器”或者按Windows键(在Deepin linux我们称为Sup ...
- 【bzoj2152】【聪聪可可】【点分治】
[问题描写叙述] 聪聪和可但是兄弟俩.他们俩常常为了一些琐事打起来,比如家中仅仅剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(但是他们家仅仅有一台电脑)--遇到这样的问题,普通情况下石头剪刀布就好 ...
- iOS 开发者中的个人账号与组织账号之间区别
苹果对开发者主要分为3类:个人.组织(公司.企业).教育机构.即: 1.个人(Individual) 2.组织(Organizations) 组织类又分为2个小类: (1)公司(Company) (2 ...
- PHP 7.2 RC3 on CentOS/RHEL 7.3 via Yum
https://webtatic.com/packages/php72/ rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-lat ...
- bzoj2461: [BeiJing2011]符环
再做水题就没救了 考虑DP...我们把正反面一起弄 fi,j,k,u表示第几个位置,正面多了多少左括号,背面多了多少没办法消的右括号,背面结尾的左括号数 #include<cstdio> ...
- ZOJ3261 Connections in Galaxy War —— 反向并查集
题目链接:https://vjudge.net/problem/ZOJ-3261 In order to strengthen the defense ability, many stars in g ...