BZOJ3072 : [Pa2012]Two Cakes
考虑DP,设$f[i][j]$表示考虑了$a[1..i]$和$b[1..j]$的最小代价。
若$a[i]==b[j]$,则$f[i][j]=\min(f[i-1][j],f[i][j-1])+1$。
否则找到最大的$t$,满足$x$和$y$往前$t$个均不相等,此时$f[i][j]=f[i-t-1][j-t-1]+t$。
对于$t$,可以通过在相应差值的序列中二分查找得到。
对于DP的计算,可以通过搜索,并将那$n$个$a[i]==b[j]$的状态记忆化。
因为对于每个没有记忆化的状态,均可以在$O(\log n)$的时间内转化为那$n$个状态,所以总时间复杂度为$O(n\log n)$。
#include<cstdio>
const int N=1000010,BUF=13778000;
int n,m,i,j,a[N],b[N],c[N],f[N],g[N<<1],nxt[N],st[N<<1],en[N<<1],q[N];char Buf[BUF],*buf=Buf;
inline void read(int&a){for(a=0;*buf<48;buf++);while(*buf>47)a=a*10+*buf++-48;}
inline void add(int x,int y){nxt[y]=g[x];g[x]=y;}
inline int min(int a,int b){return a<b?a:b;}
inline int pre(int l,int r,int x){
int t=0,mid;
while(l<=r)if(q[mid=(l+r)>>1]<=x)l=(t=mid)+1;else r=mid-1;
return q[t];
}
int dp(int x,int y){
if(!x||!y)return x+y;
if(a[x]==b[y])return f[x]?f[x]:f[x]=min(dp(x-1,y),dp(x,y-1))+1;
int t=pre(st[x-y+n],en[x-y+n],x);
return t?dp(t,y-x+t)+x-t:(x>y?x:y);
}
int main(){
fread(Buf,1,BUF,stdin);read(n);
for(i=1;i<=n;i++)read(a[i]);
for(i=1;i<=n;i++)read(b[i]),c[b[i]]=i;
for(i=n;i;i--)add(i-c[a[i]]+n,i);
for(i=1;i<n+n;en[i++]=m)for(st[i]=m+1,j=g[i];j;j=nxt[j])q[++m]=j;
return printf("%d",dp(n,n)),0;
}
BZOJ3072 : [Pa2012]Two Cakes的更多相关文章
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- hdu 5997 rausen loves cakes(线段数合并+启发式修改)
题目链接:hdu 5997 rausen loves cakes 题意: 给你n个点,每个点有一个颜色,现在有两个操作,第一个操作,将颜色x改为颜色y,第二个操作,询问[x,y]区间有多少颜色段(颜色 ...
- BZOJ3498PA2009 Cakes——三元环
题目描述 N个点m条边,每个点有一个点权a.对于任意一个三元环(j,j,k)(i<j<k),它的贡献为max(ai,aj,ak) 求所有三元环的贡献和.N<100000,,m< ...
- Codeforces Round #542 B Two Cakes
B. Two Cakes time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- [BZOJ 3498] [PA 2009] Cakes
Description \(n\) 个点 \(m\) 条边,每个点有一个点权 \(a_i\). 对于任意一个三元环 \((i,j,k)(i<j<k)\),它的贡献为 \(\max(a_i, ...
- [Codeforces-911B] - Two Cakes
B. Two Cakestime limit per test 1 secondmemory limit per test 256 megabytesinput standard inputoutpu ...
- 「BZOJ 4289」 PA2012 Tax
「BZOJ 4289」 PA2012 Tax 题目描述 给出一个 \(N\) 个点 \(M\) 条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点 \(1\) 到点 \( ...
- Codeforces Round #413 A. Carrot Cakes
A. Carrot Cakes time limit per test 1 second memory limit per test 256 megabytes In some game ...
- 【BZOJ3502/2288】PA2012 Tanie linie/【POJ Challenge】生日礼物 堆+链表(模拟费用流)
[BZOJ3502]PA2012 Tanie linie Description n个数字,求不相交的总和最大的最多k个连续子序列. 1<= k<= N<= 1000000. Sam ...
随机推荐
- 修改searchbar 取消 字体 颜色
UIButton *cancelButton; UIView *topView = self.searchDisplayController.searchBar.subviews[]; for (UI ...
- STL_advance distance prev next
template<class InputIterator> typename iterator_traits<InputIterator>::difference_type d ...
- 测试Open Live writer
这个东西究竟是怎么用的啊,看起来好高端的样子...
- Java集合源码学习(四)HashMap分析
ArrayList.LinkedList和HashMap的源码是一起看的,横向对比吧,感觉对这三种数据结构的理解加深了很多. >>数组.链表和哈希表结构 数据结构中有数组和链表来实现对数据 ...
- 【翻译十三】java-并发之饥饿与活锁
Starvation and Livelock Starvation and livelock are much less common a problem than deadlock, but ar ...
- centos6.4下安装php的imagick和imagemagick扩展教程
imagick在centos6.4的安装方法: .安装ImageMagick 代码如下: wget http://soft.vpser.net/web/imagemagick/ImageMagick- ...
- 源码方式安装mysql5.5
mysql5.5开始,源码配置编译工具configure变成了cmake,所以先要去把cmake装上.并安装make,bison,cmake,gcc-c++,ncurses的包 去http://www ...
- win7 快捷键
F F1 显示辅助 F2 重命名选定项目 F3 搜索文件或文件夹 F4 在 Windows 资源管理器中显示地址栏列表 F5 刷新活动窗口 F6 在窗口中或桌面上循环切换屏幕元素 F10 激活活动程序 ...
- jbox使用总结
jbox是一个不错的插件 当使用get打开新页面的时候,可以使用h.对像ID来获得对像ID的值 Js代码 js代码: /** * @description: test * @author: BrinP ...
- Android之Inflate()
Inflate()作用就是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的,没有找到的同时并显示功能.最近做的一个项目就是这一点让我迷茫了好几天. Android上还有一个与Inflate( ...