牛客 A.Equivalent Prefixes(单调栈)

链接:https://ac.nowcoder.com/acm/contest/881/A
来源:牛客网
 

时间限制: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

题意:定义两个数组“相等”的概念:[1,m]的范围内任取子区间[l,r]都满足区间最小值所在的位置(下标)相同[1,m]的范围内任取子区间[l,r]都满足区间最小值所在的位置(下标)相同。那么给定两个数组,问m的最大取值是多少。

做法:裸的单调栈,单调队列pa,pb可以用单调栈线性处理出来,总时间复杂度为O(n)

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;

int a[N], b[N], n, pa[N], pb[N];


int main() {
    while(~scanf("%d", &n)) {
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &a[i]);
        }
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &b[i]);
        }
        a[0] = b[0] = -1e9;
        stack<int> sa, sb;
        sa.push(0); sb.push(0);
        for(int i = 1; i <= n; ++i) {
            while(!sa.empty() && a[sa.top()] > a[i]) sa.pop();
            pa[i] = sa.top();
            sa.push(i);
            while(!sb.empty() && b[sb.top()] > b[i]) sb.pop();
            pb[i] = sb.top();
            sb.push(i);
        }
        int p = 1;
        for(int i = 1; i <= n; ++i) {
            if(pa[i] == pb[i])
                p = i;
            else
                break;
        }
        printf("%d\n", p);
    }
    return 0;
}

以下是错误代码,不用单调栈果然会使数据量太大爆掉。额我还是太年轻了。

#include<iostream>
#include<vector>
#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
const int maxn=0x3f3f3f3f;
vector<int>a,b;
vector<int>flaga,flagb;
int main(){
	int n;
	while(scanf("%d",&n)!=EOF)
	{   flaga.clear();
	    flagb.clear();
	    a.clear();
	    b.clear();int num;
	    for(int i=0;i<n;i++){
	    	cin>>num;
	    	a.push_back(num);
		}
	    for(int i=0;i<n;i++){
	    	cin>>num;
	    	b.push_back(num);
		}
	    int minna=maxn;
	    int minnb=maxn;
		for(int i=0;i<n;i++){
			if(minna>=a[i])minna=a[i],flaga.push_back(1);
			else flaga.push_back(0);	
			if(minnb>=b[i])minnb=b[i],flagb.push_back(1);
			else flagb.push_back(0);	
		}
	     minna=maxn;
	     minnb=maxn;
		for(int i=0;i<n;i++){
			if(flaga[i]!=flagb[i]){
			cout<<i<<endl;break;	
			}
			if(i==n-1)cout<<n<<endl;
		}
	
	}
	return 0;
}
发布了183 篇原创文章 · 获赞 31 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/mlm5678/article/details/96476807
今日推荐