[Usaco2017 Feb]Why Did the Cow Cross the Road II
来源:互联网 发布:ssd caffe 源码解读 编辑:程序博客网 时间:2024/05/30 05:03
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 0x7f7f7f7fusing 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;}
阅读全文
0 0
- [Usaco2017 Feb]Why Did the Cow Cross the Road II
- 4992: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4995: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4989: [Usaco2017 Feb]Why Did the Cow Cross the Road
- Bzoj4989 [Usaco2017 Feb]Why Did the Cow Cross the Road
- [Usaco2017 Feb]Why Did the Cow Cross the Road
- [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4993: [Usaco2017 Feb]Why Did the Cow Cross the Road II
- BZOJ 4993: [Usaco2017 Feb]Why Did the Cow Cross the Road II LCS
- BZOJ 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 树状数组优化LCS
- bzoj 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II
- bzoj4993 [Usaco2017 Feb]Why Did the Cow Cross the Road II
- bzoj4993: [Usaco2017 Feb]Why Did the Cow Cross the Road II
- 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II
- bzoj4996: [Usaco2017 Feb]Why Did the Cow Cross the Road II
- bzoj 4996: [Usaco2017 Feb]Why Did the Cow Cross the Road II
- bzoj4990 [Usaco2017 Feb]Why Did the Cow Cross the Road II(dp+树状数组)
- 【bzoj4990】[Usaco2017 Feb]Why Did the Cow Cross the Road II
- Split Array into Consecutive Subsequences问题及解法
- Android开发中实现数组和集合list的相互转换
- jquery中同步异步的区别与联系
- HTML DOM 事件
- unique离散化用法
- [Usaco2017 Feb]Why Did the Cow Cross the Road II
- PriorityQueue 原理与应用
- USB的NRZI信号格式
- spring mvc 从本地文件中读取图片,并显示在页面中
- U盘升级软件
- Http StatuCode说明
- WKWebView内容自适应宽度
- java 增强型 for 循环
- Linux中的文件分界符EOF