第六章例题

 1 #include <iostream>
 2 #include <stack>
 3 #include <vector>
 4 
 5 using namespace std;
 6 
 7 vector<int> vt;
 8 stack<int> st;
 9 
10 int main()
11 {
12     vt.clear();
13     
14     int n;
15     cin>>n;
16 
17     for(int i=0;i<n;i++)
18     {
19         int temp;
20         cin>>temp;
21         vt.push_back(temp);
22     }
23 
24     st.push(vt[n-1]);
25 
26     bool flag=true;
27     int j=n;
28 
29     for(int i=n-2;i>=0;i--)
30     {
31         if(vt[i]>st.top())
32             st.push(vt[i]);
33         if(vt[i]<st.top())
34         {
35             for(;;)
36             {
37                 if(st.top()!=j)
38                 {
39                     flag=false;
40                     break;
41                 }
42 
43                 st.pop();
44                 j--;
45 
46                 if(vt[i]>st.top())
47                 {
48                     st.push(vt[i]);
49                     break;
50                 }
51             }
52         }
53 
54         if(flag==false)
55             break;
56     }
57 
58     while(!st.empty())
59     {
60         if(st.top()!=j)
61         {
62             flag=false;
63             break;
64         }
65 
66         st.pop();
67         j--;
68     }
69 
70     if(flag==false)
71         cout<<"no"<<endl;
72     else
73         cout<<"yes"<<endl;
74 
75 }
Yosoro
原文地址:https://www.cnblogs.com/tclan126/p/7234395.html