poj 3250 栈应用】的更多相关文章

#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> #define Maxn 100010 using namespace std; __int64 ans=; int Stack[Maxn],top; int main() { int n,i,j,a; while(scanf("%d",&n)!=EOF) { ans=; top=;…
1.Poj 3250  Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所有牛均比i矮,i就可看到j.i可看到的所有牛数记为ai,求S(ai),(1<=i<=n). 转化一下,求j可以被多少牛看到.这样就直接单调栈,求从j往前单调递增的数量. #include<iostream> #include<cstring> #include<cma…
http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11473   Accepted: 3871 Description Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious abou…
http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15985   Accepted: 5404 Description Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious abou…
[题目链接] http://poj.org/problem?id=3250 [题目大意] 有n头牛,每头牛都有一定的高度,他能看到在离他最近的比他高的牛前面的所有牛 现在每头牛往右看,问每头牛能看到的牛的数量的总和. [题解] 单调栈维护每个数字右边第一个比其大的数字的位置,从后往前计算, 为保证最后一段计算的正确性,在最后一个位置后面加一个无限高的哨兵即可. [代码] #include <cstdio> using namespace std; const int N=80010; type…
维护一个单调栈,保持从大到小的顺序,每次加入一个元素都将其推到尽可能栈底,知道碰到一个比他大的,然后res+=tail,说明这个cow的头可以被前面tail个cow看到.如果中间出现一个超级高的,自然会推到栈底,即此元素以前的cow看不到此元素后面cow的头,即res不会加此元素前面的个数,说明是正确的. 注意要用long long 或者 __int64类型. 刚开始看着80000不大的样子,其实最大情况下res有 1+2+....+80000 = 3200040000,超过int范围.以后还是…
Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15922   Accepted: 5374 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…
Bad Hair Day 题意:给n(n <= 800,000)头牛,每头牛都有一个高度h,每头牛都只能看到右边比它矮的牛的头发,将每头牛看到的牛的头发加起来为多少? 思路:每头要进栈的牛,将栈顶比其矮的牛出栈,因为这些牛都没有机会看到更后面的牛了,所以出栈;这时加上栈中的元素个数即可: #include<iostream> #include<cstdio> #include<cstring> #include<string.h> #include&l…
Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14883   Accepted: 4940 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…
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 specified heig…