Bad Hair Day---poj3250(栈的运用)】的更多相关文章

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…
题目链接:https://vjudge.net/problem/POJ-3250 题意:求序列中每个点右边第一个>=自身的点的下标. 思路:简单介绍单调栈,主要用来求向左/右第一个小于/大于自身的下标,直接求的话复杂度为O(n2),而单调栈只有O(n),利用好单调栈十分有用.一个元素向左遍历的第一个比它小的数的位置就是将它插入单调栈时栈顶元素的值,若栈为空,则说明不存在这么一个数.然后将此元素的下标存入栈,就能类似迭代般地求解后面的元素. 单调栈的本质是,当一个数a在另一个数b前面且比b大,那么…
题目大概就是给一个序列,问每个数右边有几个连续且小于该数的数. 用单调递减栈搞搞就是了. #include<cstdio> #include<cstring> using namespace std; #define INF (1<<30) #define MAXN 88888 int a[MAXN],r[MAXN],stack[MAXN],top; int main(){ int n; scanf("%d",&n); ; i<=n;…
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…
题意Farmer John的奶牛在风中凌乱了它们的发型……每只奶牛都有一个身高hi(1 ≤ hi ≤ 1,000,000,000),现在在这里有一排全部面向右方的奶牛,一共有N只(1 ≤ N ≤ 80,000).对于奶牛i来说,如果奶牛i+1,i+2,……,N这些奶牛的身高严格小于奶牛i,则奶牛i可以看到它们凌乱的发型. 输入第一行 奶牛数量N第二到 N+1行:第i+1行输入奶牛i的身高 输出第一行:一个整数即c1到cN的和 样例输入610374122 样例输出5 分析方法一对于i,我们知道,令…
题目大意:给定一个由 N 个数组成的序列,求以每个序列为基准,向右最大有多少个数字都比它小. 单调栈 单调栈中维护的是数组的下标. 单调栈在每个元素出栈时统计该出栈元素的答案贡献或对应的值. 单调栈主要应用于区间最值的贡献问题. 代码如下 #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn=8e4+10; const int inf=0x…
Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24420   Accepted: 8292 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…
解题关键:将每头牛看到的牛头数总和转化为每头牛被看到的次数,然后用单调栈求解,其实做这道题的目的只是熟悉下单调栈 此题为递减栈 #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<stack> #include<iostream> using namespace std; typedef long long ll; ll a…
这个题再次证明了单调栈的力量 简单 单调栈 类似上次校内选拔消砖块 一堆牛面朝右排 给出从左到右的 问每个牛的能看到前面牛发型的个数之和 //re原因 因为在执行pop的时候没有判断empty 程序崩溃 转换下思想 我们可以求每个牛被后面的牛看见的次数之和 维护一个单调减栈 size为能看到当前准备入栈的发型的牛个数 so 解决了 代码超短 #include<cstdio> #include<map> //#include<bits/stdc++.h> #include…
思路: 维护一个单调递增的栈,对于栈顶元素<=新值来说,那么后面的,我一定看不到了,pop掉以后,那么这时候的栈的大小就是我能看到的这个刚刚pop出去元素的个数. //#include <bits/stdc++.h> #include<cstdio> #include<stack> #include<algorithm> using namespace std; typedef long long LL; const int N=1e5+10; int…