因为是从1-p,所以可以维护两个递增且比a[i]小的栈,如果过程中两个栈的元素数量不一样多,说明到此位置时,最小值的位置不相同。
Code
1/**
2 * author: Akvicor
3 * created: 2019-07-18 12-32-25
4**/
5
6#include <bits/stdc++.h>
7
8using namespace std;
9
10#ifdef DEBUG
11#define FAST_IO 17
12#else
13#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
14#define endl '\n'
15#endif
16
17#define LL long long
18#define ULL unsigned long long
19#define rep(i, n) for(int i = 0; i < (n); ++i)
20#define reep(i, n) for(int i = 0; i <= (n); ++i)
21#define lop(i, a, n) for(int i = a; i < (n); ++i)
22#define loop(i, a, n) for(int i = a; i <= (n); ++i)
23#define ALL(v) (v).begin(), (v).end()
24#define PB push_back
25#define VI vector<int>
26#define PII pair<int,int>
27#define FI first
28#define SE second
29#define SZ(x) ((int)(x).size())
30
31const double EPS = 1e-6;
32const double PI = acos(-1.0);
33const int INF = 0x3f3f3f3f;
34const LL LINF = 0x7f7f7f7f7f7f7f7f;
35const int MAXN = (int)1e6 + 10;
36const int MOD = (int)1e9 + 7;
37
38int n;
39int a[MAXN], b[MAXN];
40stack<int> x, y;
41
42int main(){
43 FAST_IO;
44 int ans;
45 while(cin >> n){
46 while(!x.empty()) x.pop();
47 while(!y.empty()) y.pop();
48 rep(i, n) cin >> a[i];
49 rep(i, n) cin >> b[i];
50 for(int i = 0; i < n; ++i){
51 while(!x.empty() && x.top() > a[i]) x.pop();
52 x.push(a[i]);
53 while(!y.empty() && y.top() > b[i]) y.pop();
54 y.push(b[i]);
55 if(x.size() == y.size()) ans = i;
56 else break;
57 }
58 cout << ans+1 << endl;
59 }
60
61 return 0;
62}
除另有声明外,本博客文章均采用 知识共享 (Creative Commons) 署名 4.0 国际许可协议 进行许可。转载请注明原作者与文章出处。