A.Equivalent Prefixes(ST算法)
Equivalent Prefixes
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述
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.
输入
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算法)的更多相关文章
- 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)
链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...
- 2019牛客暑期多校训练营(第一场) - A - Equivalent Prefixes - 单调栈
A - Equivalent Prefixes - 单调栈 题意:给定两个n个元素的数组a,b,它们的前p个元素构成的数组是"等价"的,求p的最大值."等价"的 ...
- ST算法
作用:ST算法是用来求解给定区间RMQ的最值,本文以最小值为例 举例: 给出一数组A[0~5] = {5,4,6,10,1,12},则区间[2,5]之间的最值为1. 方法:ST算法分成两部分:离线预处 ...
- 求解区间最值 - RMQ - ST 算法介绍
解析 ST 算法是 RMQ(Range Minimum/Maximum Query)中一个很经典的算法,它天生用来求得一个区间的最值,但却不能维护最值,也就是说,过程中不能改变区间中的某个元素的值.O ...
- RMQ问题之ST算法
RMQ问题之ST算法 RMQ(Range Minimum/Maximum Query)问题,即区间最值问题.给你n个数,a1 , a2 , a3 , ... ,an,求出区间 [ l , r ]的最大 ...
- RMQ之ST算法模板
#include<stdio.h> #include<string.h> #include<iostream> using namespace std; ; ],M ...
- CodeForces 359D (数论+二分+ST算法)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...
- RMQ问题(线段树+ST算法)
转载自:http://kmplayer.iteye.com/blog/575725 RMQ (Range Minimum/Maximum Query)问题是指:对于长度为n的数列A,回答若干询问RMQ ...
- [POJ3264]Balanced Lineup(RMQ, ST算法)
题目链接:http://poj.org/problem?id=3264 典型RMQ,这道题被我鞭尸了三遍也是醉了…这回用新学的st算法. st算法本身是一个区间dp,利用的性质就是相邻两个区间的最值的 ...
随机推荐
- F. Greedy Sequence(主席树区间k的后继)(The Preliminary Contest for ICPC Asia Nanjing 2019)
题意: 查找区间k的后继. 思路: 直接主席树. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <cstdio&g ...
- spark教程(11)-sparkSQL 数据抽象
数据抽象 sparkSQL 的数据抽象是 DataFrame,df 相当于表格,它的每一行是一条信息,形成了一个 Row Row 它是 sparkSQL 的一个抽象,用于表示一行数据,从表现形式上看, ...
- LOJ题解#136. 最小瓶颈路 DFS+Kruskal
题目链接: https://loj.ac/problem/136 思路: 在我的这篇博客中已经讲到什么是最短瓶颈路,同时给出了一个用Kruskal求最短瓶颈路的一个简洁易懂的方法,然而这道题目可以看作 ...
- centos安装配置php
PHP的安装同样需要经过环境检查.编译和安装3个步骤. 1.首先用百度搜索 “PHP:Downloads”, 点击第一个网页: 选择5.5.37版本,选择 .tar.gz 格式的文件: 来到镜像列表网 ...
- with as 语句
with就是一个sql片段,供后面的sql语句引用. 详情参见:https://www.cnblogs.com/Niko12230/p/5945133.html
- 访问接口错误,com.netflix.client.ClientException: Load balancer does not have available server for client: panfeng-item-service
com.netflix.client.ClientException: Load balancer does not have available server for client: panfeng ...
- ERP人员组织岗位权限菜单关系视图
- CentOS7 配置NFS(Network File System)及其使用
1. 服务端配置 1.1. 安装NFS yum -y install nfs* 1.2. 查看是否安装了NFS与RPCBIND rpm -qa | grep nfs rpm - ...
- xml配置文件命名空间学习
xmlns(XML Namespaces的缩写)是一个属性,是XML(标准通用标记语言的子集)命名空间.作用是赋予命名空间一个唯一的名称. 编写Spring或者Maven或者其他需要用到XML文档的程 ...
- label smooth
图像分类的一个trick,推导可参考这位博主https://leimao.github.io/blog/Label-Smoothing/ 知乎上的讨论https://www.zhihu.com/que ...