bnuoj 25659 A Famous City (单调栈)
http://www.bnuoj.com/bnuoj/problem_show.php?pid=25659
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <math.h> 5 #include <algorithm> 6 #include <stack> 7 8 using namespace std; 9 10 int main() 11 { 12 int n,cas=1; 13 while(~scanf("%d",&n)) 14 { 15 int i; 16 stack<int> stk; 17 stk.push(-1); 18 int ans = 0; 19 for(i=0;i<n;i++) 20 { 21 int temp; 22 scanf("%d",&temp); 23 while(stk.top()>temp) 24 { 25 stk.pop(); 26 } 27 if(stk.top()!=temp) 28 { 29 stk.push(temp); 30 if(temp!=0) ans++; 31 } 32 } 33 while(!stk.empty()){stk.pop();} 34 printf("Case %d: %d\n",cas++,ans); 35 } 36 return 0; 37 }