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

题目链接 https://ac.nowcoder.com/acm/contest/881/A

题意:给你2个数组,求最大的p使得 2个数组1到p的任何一个子区间最小值下标相同

题解:设1到p的最小值下标为x,仅需满足x+1到p 2个数组对应值的大小排序相同

使用单调栈从小到大排:最开始只有一列,是最小的,如果后面有比栈底元素小的,栈会被弹空,把最小的压入栈,如果后面来的数据在栈中排序不同的话,2个栈弹栈次数不同,比较2个栈数据的数量就可以判断当前加入的值满不满足

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>

using namespace std;

const int N = 1e5 + 5;
stack<int> Ma;
stack<int> Mb;
int a[N];
int b[N];
int n;

void initMa() {
	while (!Ma.empty()) Ma.pop();
}
void initMb() {
	while (!Mb.empty()) Mb.pop();
}

int main() {
	while (cin>>n) {
		for (int i = 0; i < n; i++) scanf("%d", a + i);
		for (int i = 0; i < n; i++) scanf("%d", b + i);
		if (!Ma.empty()) initMa();
		if (!Mb.empty()) initMb();
		Ma.push(a[0]);
		Mb.push(b[0]);
		int k = 1;
		for (int i = 1; i < n; i++) {
			while (1) {
				if (Ma.empty() || Ma.top() < a[i]) {
					Ma.push(a[i]);
					break;
				}
				else Ma.pop();
			}
			while (1) {
				if (Mb.empty() || Mb.top() < b[i]) {
					Mb.push(b[i]);
					break;
				}
				else Mb.pop();
			}
			if (Ma.size() != Mb.size()) break;
			k++;
		}
		cout << k << endl;
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值