bzoj4990 [Usaco2017 Feb]Why Did the Cow Cross the Road II(dp+树状数组)
来源:互联网 发布:电脑安装网络调试等 编辑:程序博客网 时间:2024/05/18 01:12
dp[j]表示第二行选择的最后一个位置为j时,最多选择多少对。以第一行做到i划分阶段,则这次只会影响a[i]能连到的的位置j,
dp[j]=max{dp[k]| 0<=k< j}+1.这样复杂度是O(n2*9)的,过不了,我们可以用树状数组或线段树维护一下决策的前缀最大值,优化成O(9*nlogn)。
#include <bits/stdc++.h>using namespace std;#define ll long long#define N 100010#define inf 0x3f3f3f3finline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f;}int n,a[N],dp[N],c[N],dx[]={-4,-3,-2,-1,0,1,2,3,4};vector<int>to[N];inline void update(int x,int val){ for(;x<=n;x+=x&(-x)) c[x]=max(c[x],val);}inline int ask(int x){ int res=0; for(;x;x-=x&(-x)) res=max(res,c[x]);return res;}int main(){// freopen("a.in","r",stdin); n=read();for(int i=1;i<=n;++i) a[i]=read(); for(int j=1;j<=n;++j){ int x=read(); for(int k=0;k<9;++k){ int xx=x+dx[k];if(xx<1||xx>n) continue;to[xx].push_back(j); } }for(int i=1;i<=n;++i){ for(int j=to[a[i]].size()-1;j>=0;--j){ dp[to[a[i]][j]]=ask(to[a[i]][j]-1)+1;update(to[a[i]][j],dp[to[a[i]][j]]); } }int ans=0;for(int i=1;i<=n;++i) ans=max(ans,dp[i]); printf("%d\n",ans); return 0;}
阅读全文
0 0
- bzoj4990 [Usaco2017 Feb]Why Did the Cow Cross the Road II(dp+树状数组)
- 【bzoj4990】[Usaco2017 Feb]Why Did the Cow Cross the Road II
- BZOJ4990&&BZOJ4993(Usaco2017 Feb)[Why Did the Cow Cross the Road II]--最长升
- BZOJ 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 树状数组优化LCS
- [Usaco2017 Feb]Why Did the Cow Cross the Road II
- bzoj 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组套Treap
- [BZOJ]4991: [Usaco2017 Feb]Why Did the Cow Cross the Road III CDQ分治+树状数组
- 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 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组套splay【未AC】
- bzoj4989 [Usaco2017 Feb]Why Did the Cow Cross the Road(树状数组求逆序对个数)
- 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
- Cmake3.10.0+64位win10+vs2015配置OpenCv3.3.0
- 输出两个数的最大值
- Java 的线程池 ExecutorService 简单介绍
- Spring Boot CLI安装及使用。
- angularjs添加列表和排序
- bzoj4990 [Usaco2017 Feb]Why Did the Cow Cross the Road II(dp+树状数组)
- 正则表达式
- c语言中的数据类型
- AngularJs路由与添加用户(1)
- 数组的螺旋式打印
- spring mybatis batch操作时关闭sqlsession
- Linux基础练习题(7)
- 数据分析-Numpy模块
- 数据结构 顺序表