CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列
B. Ancient Berland Hieroglyphs
题目连接:
http://codeforces.com/problemset/problem/164/B
Descriptionww.co
Polycarpus enjoys studying Berland hieroglyphs. Once Polycarp got hold of two ancient Berland pictures, on each of which was drawn a circle of hieroglyphs. We know that no hieroglyph occurs twice in either the first or the second circle (but in can occur once in each of them).
Polycarpus wants to save these pictures on his laptop, but the problem is, laptops do not allow to write hieroglyphs circles. So Polycarp had to break each circle and write down all of its hieroglyphs in a clockwise order in one line. A line obtained from the first circle will be called a, and the line obtained from the second one will be called b.
There are quite many ways to break hieroglyphic circles, so Polycarpus chooses the method, that makes the length of the largest substring of string a, which occurs as a subsequence in string b, maximum.
Help Polycarpus — find the maximum possible length of the desired substring (subsequence) if the first and the second circles are broken optimally.
The length of string s is the number of characters in it. If we denote the length of string s as |s|, we can write the string as s = s1s2... s|s|.
A substring of s is a non-empty string x = s[a... b] = sasa + 1... sb (1 ≤ a ≤ b ≤ |s|). For example, "code" and "force" are substrings of "codeforces", while "coders" is not.
A subsequence of s is a non-empty string y = s[p1p2... p|y|] = sp1sp2... sp|y| (1 ≤ p1 < p2 < ... < p|y| ≤ |s|). For example, "coders" is a subsequence of "codeforces".
Input
The first line contains two integers la and lb (1 ≤ la, lb ≤ 1000000) — the number of hieroglyphs in the first and second circles, respectively.
Below, due to difficulties with encoding of Berland hieroglyphs, they are given as integers from 1 to 106.
The second line contains la integers — the hieroglyphs in the first picture, in the clockwise order, starting with one of them.
The third line contains lb integers — the hieroglyphs in the second picture, in the clockwise order, starting with one of them.
It is guaranteed that the first circle doesn't contain a hieroglyph, which occurs twice. The second circle also has this property.
Output
Print a single number — the maximum length of the common substring and subsequence. If at any way of breaking the circles it does not exist, print 0.
Sample Input
5 4
1 2 3 4 5
1 3 5 6
Sample Output
2
Hint
题意
给你两个字符串
这两个字符串都是环状的
你需要在第一个字符串里面找到一个最长子串是第二个字符串的子序列
题解:
单调队列,队列里面存的是从小到大的a[i]的位置
我们队列维护的时候,以a[i]这个数结尾的话,能够得到的最大的长度
由于这个东西是个环,所以我们直接让第二个字符串转到他的前面就好了,这样就所有的位置都小于他了
比如你队列的数字已经是2 5 7了,m为6。
现在你要插入一个6怎么办?
因为6<7,所以我们6+m = 12,然后把12插进去就好了
由于2<12-m 5<12-m,所以我们把2和5都弹出去。
队列就变成了 7 12
如果再插一个5怎么办,我们让5 + 2*m就好了
再弹出去7
就变成12 17
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6*2+5;
int n,m;
int a[maxn];
int b[maxn];
int c[maxn];
int main()
{
memset(c,-1,sizeof(c));
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<m;i++)
{
scanf("%d",&b[i]);
c[b[i]]=i;
}
int ans = 0;
queue<long long> Q;
for(int i=0;i<2*n;i++)
{
long long p = c[a[i%n]];
if(p==-1)while(!Q.empty())Q.pop();
else{
if(Q.size()&&p<=Q.back())
p+=(Q.back()-p+m)/m*m;
Q.push(p);
while(p-Q.front()>=m)
Q.pop();
}
ans=max(ans,(int)Q.size());
}
printf("%d\n",ans);
}
CodeForces 164 B. Ancient Berland Hieroglyphs 单调队列的更多相关文章
- Codeforces 1 C. Ancient Berland Circus-几何数学题+浮点数求gcd ( Codeforces Beta Round #1)
C. Ancient Berland Circus time limit per test 2 seconds memory limit per test 64 megabytes input sta ...
- CodeForces - 1C:Ancient Berland Circus (几何)
Nowadays all circuses in Berland have a round arena with diameter 13 meters, but in the past things ...
- Codeforces 1304F1/F2 Animal Observation(单调队列优化 dp)
easy 题目链接 & hard 题目链接 给出一张 \(n \times m\) 的矩阵,每个格子上面有一个数,你要在每行选出一个点 \((i,t)\),并覆盖左上角为 \((i,t)\), ...
- Codeforces Beta Round #6 (Div. 2 Only) 单调队列
题目链接: http://codeforces.com/contest/6/problem/E E. Exposition time limit per test 1.5 secondsmemory ...
- Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列
B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...
- Codeforces 445A Boredom(DP+单调队列优化)
题目链接:http://codeforces.com/problemset/problem/455/A 题目大意:有n个数,每次可以选择删除一个值为x的数,然后值为x-1,x+1的数也都会被删除,你可 ...
- Codeforces Beta Round #1 C. Ancient Berland Circus 计算几何
C. Ancient Berland Circus 题目连接: http://www.codeforces.com/contest/1/problem/C Description Nowadays a ...
- Codeforces 1029B. Creating the Contest 动态规划O(nlogn)解法 及 单调队列O(n)解法
题目链接:http://codeforces.com/problemset/problem/1029/B 题目大意:从数组a中选出一些数组成数组b,要求 b[i+1]<=b[i]*2 . 一开始 ...
- Codeforces 940 E.Cashback (单调队列,dp)
Codeforces 940 E.Cashback 题意:一组数,要分为若干个区间,每个区间长度为ki(1<=ki<=n),并且对于每个区间删去前ki/c(向下取整)个小的数(即对区间升序 ...
随机推荐
- 我常用的Linux命令
CD: .. —-切换到上层目录 ~ —-回到家目录(/home/你的登录名/) LS: -a —-显示指定目录所有文件,包括文件名以 . 开头的文件 -l ...
- Raspberry Pi3 ~ 搭建开发环境
关于树莓派的开发环境 纠结了一些时间 ,我的是raspberry Pi 3 mode b 在官网下载 noobs (raspbain 版本)的. 安装完成之后接上显示器 启动系统 然后最初我是想在这个 ...
- 如何使用Fiddler调试线上JS代码
大家平时肯定都用过火狐的Firebug或者谷歌的调试工具来调试JS,但遗憾的是我们不能像编辑html,css那样来直接新增或者删除JS代码. 虽然可以通过调试工具的控制台来动态执行JS代码,但有时候却 ...
- 写好Hive 程序的五个提示
转自http://www.alidata.org/archives/622 使用Hive可以高效而又快速地编写复杂的MapReduce查询逻辑.但是某些情况下,因为不熟悉数据特性,或没有遵循Hive的 ...
- iframe和frame的区别
在同时有frame和Iframe的一个窗口里frame最大可以做个frameset的儿子,Iframe最大也只能做到frameset的孙子.frame的布局限于几种,Iframe想放哪里放哪里.fra ...
- rm 注意
软连接ln -s lnfile file rm -rf lnfile只是删除lnfile ln -s lndir dir rm -rf lndir 删除链接 rm -rf lndir/删除目录下文件
- ubuntu下apt-get update出现hash校验和错误
可能原因 校园网进行网络缓存导致内容滞后. 解决办法 先清除旧的apt-get更新列表 sudo rm -rf /var/lib/apt/lists/* 使用代理服务器或者VPN 重新更新 sudo ...
- SharePoint咨询师之路:设计之前的那些事四:负载均衡 - web服务器
提示:本系列只是一个学习笔记系列,大部分内容都可以从微软官方网站找到,本人只是按照自己的学习路径来学习和呈现这些知识.有些内容是自己的经验和积累,如果有不当之处,请指正. 容量管理 规模 体系结构 ...
- 第三百五十二天 how can I 坚持
如果要是今年找不到对象,明年去回济南, 怎么感觉那么不舍呢.生活总是有太多的无奈啊. 今天加了一天,倒是没感觉,只是感觉生活太空虚. 或许遗憾只是因为自己太懦弱.怎么说呢,还是那句话,经历的就会成长, ...
- 在WP8上搭建cocos2d-x开发环境
在WP8上搭建cocos2d-x开发环境 转自:https://github.com/koowolf/cocos-docs/blob/master/manual/framework/native/in ...