51Nod 1272 最大距离 (栈)

 1 #include <cstdio>
 2 #include <queue>
 3 #include <cstring>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <stack>
 7 using namespace std;
 8 
 9 typedef long long ll;
10 struct node{int v, id;};
11 stack<node>q, sq;
12 
13 int main(){
14     int n;
15     scanf("%d", &n);
16     while(!sq.empty())  sq.pop();
17     int ans = 0;
18     for(int i = 0;i < n;i++){
19         int x;
20         scanf("%d", &x);
21         node a;
22         a.v = x; a.id = i;
23         if(sq.size() == 0 || sq.top().v > x){
24             sq.push(a);
25         }
26         else{
27             while(sq.top().v <= x){
28                 ans = max(ans, i - sq.top().id);
29                 q.push(sq.top());
30                 sq.pop();
31                 if(sq.empty())
32                     break;
33             }
34             while(!q.empty()){
35                 sq.push(q.top());
36                 q.pop();
37             }
38         }
39     }
40     printf("%d\n", ans);
41     return 0;
42 }

猜你喜欢

转载自www.cnblogs.com/jaydenouyang/p/8987552.html