[poj3250]单调栈 Bad Hair Day

解题关键:将每头牛看到的牛头数总和转化为每头牛被看到的次数,然后用单调栈求解,其实做这道题的目的只是熟悉下单调栈

此题为递减栈

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cstdlib>
 5 #include<stack>
 6 #include<iostream>
 7 using namespace std;
 8 typedef long long ll;
 9 ll a[80002];
10 stack<ll>ss;
11 int main(){
12     ll n,ans;
13     while(cin>>n){
14         ans=0;
15         for(int i=0;i<n;i++) cin>>a[i];
16         for(int i=0;i<n;i++){
17             while(!ss.empty()&&a[i]>=ss.top()){
18                 ss.pop();
19             }
20             ans+=ss.size();
21             ss.push(a[i]);
22         }
23         cout<<ans<<endl;
24         while(!ss.empty())    ss.pop();
25     }
26     return 0;
27 } 

数组实现:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cstdlib>
 4 #include<algorithm>
 5 #include<iostream>
 6 using namespace std;
 7 typedef long long ll;
 8 ll ss[80002],top;
 9 int main(){
10     ios::sync_with_stdio(0);
11     ll n,t,ans=0;
12     while(cin>>n){
13         top=-1,ans=0;
14         for(int i=0;i<n;i++){
15             cin>>t;
16             while(top!=-1&&ss[top]<=t) top--;
17             ans+=top+1;
18             ss[++top]=t;
19         }
20         cout<<ans<<endl;
21     }
22     return 0;
23 }
时间: 2024-12-11 01:08:46

[poj3250]单调栈 Bad Hair Day的相关文章

POJ3250(单调栈)

Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17614   Accepted: 5937 Description Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants

POJ3250[USACO2006Nov]Bad Hair Day[单调栈]

Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17774   Accepted: 6000 Description Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants

POJ3250---Bad Hair Day(单调栈)

Description Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows' heads. Each cow i has a sp

单调栈练习题题解

单调栈 单调栈顾名思义就是让栈中的元素是单调的,要么递增,要么递减.同样它也满足栈的性质,先进后出. 单调递增栈,则从栈顶到栈底的元素是严格递增的 单调递减栈,则从栈顶到栈底的元素是严格递减的 练习题 单调栈 练习题 POJ3250 POJ2796 BZOJ1113 HDU1506 POJ2559 JDFZ2997 POJ3250 POJ3250传送门 对于每一个牛来说,能看到的数目为向右数身高比它小的个数,累加就是答案. 所以可以倒着维护一个单调递增的栈,记录i之前的弹栈数目,累加. (正着也

(单调栈)poj-2559 Largest Rectangle in a Histogram

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the

【单调栈】hdu1506 Largest Rectangle in a Histogram

单调栈的介绍及一些基本性质 http://blog.csdn.net/liujian20150808/article/details/50752861 依次把矩形塞进单调栈,保持其单增,矩形中的元素是一个三元组,存储其位置,高度,以及以其为高度的情况下,大矩形的左边界最多扩展到哪里. 每次将新的元素塞进栈的时候,其左边界就是其左侧第一个小于它的矩形的位置+1. 然后,每个矩形出栈的时候,记录其右边界为当前往栈里面塞的矩形的位置-1,然后更新答案即可. 注意最后把所有的矩形出栈,更新答案. #in

BZOJ 3238 AHOI 2013 差异 后缀数组+单调栈

题目大意: 思路:一看各种后缀那就是后缀数组没跑了. 求出sa,height之后就可以乱搞了.对于height数组中的一个值,height[i]来说,这个值能够作为lcp值的作用域只在左边第一个比他小的位置到右边第一个比他小的位置.这个东西很明显可以倍增RMQ+二分/单调栈. 之后就是数学题了 Σlen[Ti] + len[Tj] = (len + 1) * len * (len - 1),之后吧所有求出来的Σ2 * lcp(Ti,Tj)减掉就是答案. 记得答案开long long CODE:

51nod 1215 数组的宽度&amp;poj 2796 Feel Good(单调栈)

单调栈求每个数在哪些区间是最值的经典操作. 把数一个一个丢进单调栈,弹出的时候[st[top-1]+1,i-1]这段区间就是弹出的数为最值的区间. poj2796 弹出的时候更新答案即可 #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> #include<cmath

HDU 5033---Building(单调栈)

题目链接 Problem Description Once upon a time Matt went to a small town. The town was so small and narrow that he can regard the town as a pivot. There were some skyscrapers in the town, each located at position xi with its height hi. All skyscrapers loc