Equivalent Prefixes

时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld

题目描述

Two arrays u and v each with m distinct elements are called equivalent if and only if RMQ(u,l,r)=RMQ(v,l,r)RMQ(u,l,r)=RMQ(v,l,r) for all 1≤l≤r≤m1≤l≤r≤m
where RMQ(w,l,r)RMQ(w,l,r) denotes the index of the minimum element among wl,wl+1,…,wrwl,wl+1,…,wr.
Since the array contains distinct elements, the definition of minimum is unambiguous.

Bobo has two arrays a and b each with n distinct elements. Find the maximum number p≤np≤n where {a1,a2,…,ap}{a1,a2,…,ap} and {b1,b2,…,bp}{b1,b2,…,bp} are equivalent.

输入描述:

The input consists of several test cases and is terminated by end-of-file.

The first line of each test case contains an integer n.
The second line contains n integers a1,a2,…,ana1,a2,…,an.
The third line contains n integers b1,b2,…,bnb1,b2,…,bn. * 1≤n≤1051≤n≤105
* 1≤ai,bi≤n1≤ai,bi≤n
* {a1,a2,…,an}{a1,a2,…,an} are distinct.
* {b1,b2,…,bn}{b1,b2,…,bn} are distinct.
* The sum of n does not exceed 5×1055×105.

输出描述:

For each test case, print an integer which denotes the result.
示例1

输入

2
1 2
2 1
3
2 1 3
3 1 2
5
3 1 5 2 4
5 2 4 3 1

输出

1
3
4 算法:ST表 思路:设置最小数的下标为pos = 0,依次添加一组数,并于前面的最小数进行比较,看此数是否符合条件,每次添加一组数有三种情况。
   第一种:这组数全部小于最小数,这组数是可以的,更新最小数下标,判断下一组数。
   第二种:这组数全部大于最小数,递归判断区间(pos + 1, r)里是否有最小数,如果有继续递归,直到l >= r时,返回true。如果没有返回false。
   第三种:剩余的只有一种可能了,既有大于,也有小于,显然,这种可能时不存在的,直接跳出循环,输出结果。
#include <iostream>
#include <cstdio>
#include <cmath> using namespace std; typedef unsigned long long ull; int a[];
int b[];
int pos, n;
int dpa[][][]; //三种状态: 1、当前区间的首元素的下标
// 2、从首元素开始延伸的的长度
// 3、0表示我当前期间内的最小值,1表示的当前区间内最小值的下标
int dpb[][][]; void ST_init() {
for(int i = ; i < n; i++) {
dpa[i][][] = a[i];
dpa[i][][] = i;
dpb[i][][] = b[i];
dpb[i][][] = i;
}
int nlen = (int)(log((double)(n)) / log(2.0));
for(int j = ; j <= nlen; j++) {
for(int i = ; i < n; i++) {
if(dpa[i][j - ][] < dpa[i + ( << (j - ))][j - ][]) {
dpa[i][j][] = dpa[i][j - ][];
dpa[i][j][] = dpa[i][j - ][];
} else {
dpa[i][j][] = dpa[i + ( << (j - ))][j - ][];
dpa[i][j][] = dpa[i + ( << (j - ))][j - ][];
}
if(dpb[i][j - ][] < dpb[i + ( << (j - ))][j - ][]) {
dpb[i][j][] = dpb[i][j - ][];
dpb[i][j][] = dpb[i][j - ][];
} else {
dpb[i][j][] = dpb[i + ( << (j - ))][j - ][];
dpb[i][j][] = dpb[i + ( << (j - ))][j - ][];
}
}
}
} bool ST_query(int l, int r) {
if(l >= r) { //当查询区间小于1时,表示可行
return true;
}
int k = (int)(log((double)(r - l + )) / log(2.0));
int mina;
int minb;
if(dpa[l][k][] < dpa[r - ( << k) + ][k][]) {
mina = dpa[l][k][];
} else {
mina = dpa[r - ( << k) + ][k][];
}
if(dpb[l][k][] < dpb[r - ( << k) + ][k][]) {
minb = dpb[l][k][];
} else {
minb = dpb[r - ( << k) + ][k][];
}
if(mina == minb) {
return ST_query(mina + , r);
}
return false;
} int main() {
while(~scanf("%d", &n)) {
for(int i = ; i < n; i++) {
scanf("%d", &a[i]);
}
for(int i = ; i < n; i++) {
scanf("%d", &b[i]);
}
ST_init();
pos = ;
int i;
for(i = ; i < n; i++) {
if(a[pos] > a[i] && b[pos] > b[i]) {
pos = i;
} else if(a[pos] < a[i] && b[pos] < b[i]) {
if(!ST_query(pos + , i)) {
break;
}
} else {
break;
}
}
printf("%d\n", i);
}
return ;
}

A.Equivalent Prefixes(ST算法)的更多相关文章

  1. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  2. 2019牛客暑期多校训练营(第一场) - A - Equivalent Prefixes - 单调栈

    A - Equivalent Prefixes - 单调栈 题意:给定两个n个元素的数组a,b,它们的前p个元素构成的数组是"等价"的,求p的最大值."等价"的 ...

  3. ST算法

    作用:ST算法是用来求解给定区间RMQ的最值,本文以最小值为例 举例: 给出一数组A[0~5] = {5,4,6,10,1,12},则区间[2,5]之间的最值为1. 方法:ST算法分成两部分:离线预处 ...

  4. 求解区间最值 - RMQ - ST 算法介绍

    解析 ST 算法是 RMQ(Range Minimum/Maximum Query)中一个很经典的算法,它天生用来求得一个区间的最值,但却不能维护最值,也就是说,过程中不能改变区间中的某个元素的值.O ...

  5. RMQ问题之ST算法

    RMQ问题之ST算法 RMQ(Range Minimum/Maximum Query)问题,即区间最值问题.给你n个数,a1 , a2 , a3 , ... ,an,求出区间 [ l , r ]的最大 ...

  6. RMQ之ST算法模板

    #include<stdio.h> #include<string.h> #include<iostream> using namespace std; ; ],M ...

  7. CodeForces 359D (数论+二分+ST算法)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...

  8. RMQ问题(线段树+ST算法)

    转载自:http://kmplayer.iteye.com/blog/575725 RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ ...

  9. [POJ3264]Balanced Lineup(RMQ, ST算法)

    题目链接:http://poj.org/problem?id=3264 典型RMQ,这道题被我鞭尸了三遍也是醉了…这回用新学的st算法. st算法本身是一个区间dp,利用的性质就是相邻两个区间的最值的 ...

随机推荐

  1. CSP-J 2019 T3 纪念品

    \(\mathfrak{a}\).反思: 通过这道题成功发现自己的背包还是很差\(w\): 可能这是我\(gu\)了好久好久博客的报应叭 就在做这个题的时候,自己连背包\(dp\)的思想都忘了 背包可 ...

  2. CSP 俄罗斯方块(201604-2)

    问题描述 俄罗斯方块是俄罗斯人阿列克谢·帕基特诺夫发明的一款休闲游戏. 游戏在一个15行10列的方格图上进行,方格图上的每一个格子可能已经放置了方块,或者没有放置方块.每一轮,都会有一个新的由4个小方 ...

  3. 矩阵快速幂(Matrix_Fast_Power)

    一.基础知识(1)矩阵乘法 https://blog.csdn.net/weixin_43272781/article/details/82899737 简单的说矩阵就是二维数组,数存在里面,矩阵乘法 ...

  4. 物联网的语言c,python,go等

    日本生鱼片 电热水器的使用方法http://www.hiry.cn/b/mt/33959.html 物联网层次很多,首先要看你从事哪个层级的工作了.既然你问语言,那么肯定是开发类的工作,开发类的对象中 ...

  5. GO语言(golang)官方网站!

    GO语言官方网站,在上面可以查看所有API文档.使用在线工具编写程序,你可以去看看!! https://golang.org/

  6. 工具安装——linux下安装JDK1.8

    1.查看Linux环境自带JDK 使用命令:# rpm -qa|grep gcj 显示内容其中包含相应信息# java-x.x.x-gcj-compat-x.x.x.x-xxjpp# java-x.x ...

  7. Shell脚本之sed详解

    在编写shell脚本的过程中,我们经常需要使用sed流编辑器和awk对文本文件进行处理. 一.什么是sed? sed 是一种在线编辑器,它一次处理一行内容.sed是非交互式的编辑器.它不会修改文件,除 ...

  8. python 有用的库

    1.Faker pip3 install faker官网: https://faker.readthedocs.io/en/master/providers.htmlgithub: https://g ...

  9. 解决xshell小键盘输入时串码(干货!!)

    点击文件——属性 点击终端,修改为Linux即可

  10. 关于python3.8的一些新特性的解析与代码演示

    python3.8测试版出来了,我们来介绍一些变动,代码演示一下,当然底层相关的细节变动就不介绍了 只允许传位置参数 还记得如果我们想让某些参数只能以关键字参数的方式传递该怎么做吗? def foo1 ...