这个题实际可以由一个IDEA出发解决:如果单调性一直相同,那么这两个数组的整个范围内都是等价的。因为当两个函数的一阶导数以相同的方式变化(同号)时(假设原数列是一个函数,且连续可导),最大最小值总是在相同的位置出现。既然单调性是一个非此即彼的问题,那么只需要判断递增即可(由于是要求最小值,没法再递减),从一种单调性的角度出发,就很容易联想到单调栈这个数据结构。
也就是说,只需要找这两个数组中的元素是否在同一个位置递增即可。
所以有如下思路:
- 构造两个递增的单调栈,对数组中所有元素逐个判断
- 根据两个单调栈的元素个数关系判定当前情况是否满足题意
代码:
#include<pch.h>
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include <map>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#define lldin(a) scanf_s("%lld", &a)
#define println(a) printf("%lld\n", a)
#define reset(a, b) memset(a, b, sizeof(a))
const int INF = 0x3f3f3f3f;
using namespace std;
const double PI = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod = 1000000007;
const int tool_const = 19991126;
const int tool_const2 = 33;
inline ll lldcin()
{
ll tmp = 0, si = 1;
char c;
c = getchar();
while (c > '9' || c < '0')
{
if (c == '-')
si = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
{
tmp = tmp * 10 + c - '0';
c = getchar();
}
return si * tmp;
}
///Untersee Boot IXD2(1942)
/**Although there will be many obstructs ahead,
the desire for victory still fills you with determination..**/
/**Last Remote**/
ll a[500000], b[500000];
stack<ll>sta, stb;
int DETERMINATION()
{
ios::sync_with_stdio(false);
ll n;
while (cin >> n)
{
while (sta.empty() == false)
sta.pop();
while (stb.empty() == false)
stb.pop();
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> b[i];
sta.push(a[1]);//区间上单独一个点可以说递增的一部分也可以是递减的一部分
stb.push(b[1]);
bool sign = false;
for (int i = 2; i <= n; i++)
{
while (sta.empty() == false && sta.top() > a[i])//当前元素与栈顶元素不构成单调递增关系
sta.pop();//驱逐元素
sta.push(a[i]);
while (stb.empty() == false && stb.top() > b[i])
stb.pop();
stb.push(b[i]);
if (stb.size() != sta.size())//如果两个数组的单调性在这个时候出现了不同的情况
{
sign = true;
cout << i - 1 << endl;
break;
}
}
if (!sign)
cout << n << endl;//如果全体符合单调性的一致性
}
return 0;
}