Bad Hair Day

Bad Hair Day

我的第一道单调栈题目,知道单调栈点这里的思想,思路还是比较好想的;

AC_Code:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <iomanip>
 4 #include <stack>
 5 using namespace std;
 6 typedef long long ll;
 7 const int maxn=8e4+10;
 8 
 9 stack<ll>sta,s;
10 ll num,n;
11 int main()
12 {
13     scanf("%lld",&n);
14     for(ll i=1;i<=n;i++){
15         ll h;
16         scanf("%lld",&h);
17         if( sta.empty()) sta.push(h),s.push(i);
18         else{
19             while(!sta.empty() ){
20                 if( h>=sta.top() ){
21                     ll f=s.top();s.pop();sta.pop();
22                     num+=i-f-1;
23                 }
24                 else break;
25             }
26             sta.push(h);s.push(i);
27         }
28     }
29 
30     sta.pop();s.pop();
31     while( !sta.empty() ){
32         ll f=s.top();s.pop();sta.pop();
33         num+=n-f;
34     }
35     printf("%lld\n",num);
36     return 0;
37 }

 

posted @ 2020-05-20 22:15  swsyya  阅读(164)  评论(0编辑  收藏  举报

回到顶部