https://ac.nowcoder.com/acm/contest/881/A
主要还是菜啊,比赛的时候对友用线段树过了,赛后看了好多题解总结的思路,类似于笛卡尔树的解法,利用栈来解决这个问题,很巧妙啊,大致题意就是给你两个元素数目均为n的a,b数组求出1-p(1<=p<=n)中元素中最小元素下标相同的最大p值。但是如果遇到一个不和要求的p就直接break输出此时的上一次的下标即可。
在此附上AC代码:
#include<bits/stdc++.h>
using namespace std;
stack<int>sta,stb;
int a[500005];
int b[500005];
int main()
{
int n;
while(~scanf("%d",&n))
{
int ans = 0;//ans这里是用来计算下标值的,计算进去一个就加一
for(int i = 1;i <= n;i++)
{
scanf("%d",&a[i]);
}
for(int i = 1;i <= n;i++)
{
scanf("%d",&b[i]);
}
for(int i = 1;i <= n;i++)
{
while(!sta.empty() && sta.top() > a[i])//找到数组a最小值的下标
{
sta.pop();
}
sta.push(a[i]);
while(!stb.empty() && stb.top() > b[i])//找到数组b最小值的下标
{
stb.pop();
}
stb.push(b[i]);
if(sta.size() != stb.size())//此时代表最小值的下标不一样了,所以break
{
break;
}
else
{
ans++;
}
}
printf("%d\n",ans);
while(!sta.empty()) sta.pop();
while(!stb.empty()) stb.pop();
}
}