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

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…
Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24112   Accepted: 8208 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 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15699   Accepted: 5255 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<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=;…
题目链接:http://poj.org/problem?id=3250 思路分析:题目要求求每头牛看见的牛的数量之和,即求每头牛被看见的次数和:现在要求如何求出每头牛被看见的次数? 考虑到对于某头特定的牛来说,看见它的牛一定在它的左边,另外其高度应该大于该牛的高度,所以只需要计算在其左边并高度大于它的牛的数目即可: 考虑构建一个栈,在某头牛入栈时,弹出栈中高度小于它的牛,剩下的牛高度大于它,此时计算栈的长度就可以得到该牛被看见的次数. 代码如下: #include<iostream> #inc…
题目链接:http://poj.org/problem?id=3159 题意:给出m给 x 与y的关系.当中y的糖数不能比x的多c个.即y-x <= c  最后求fly[n]最多能比so[1] 多多少糖? 差分约束问题, 就是求1-n的最短路,  队列实现spfa 会超时了,改为栈实现,就可以 有负环时,用栈比队列快 数组开小了,不报RE,报超时 ,我晕 #include <iostream> #include <cstdlib> #include <cstdio>…
http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15956   Accepted: 5391 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…
Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20408   Accepted: 5632 Case Time Limit: 1000MS   Special Judge Description Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated…
Description A math instructor is too lazy to grade a question in the exam papers in which students are supposed to produce a complicated formula for the question asked. Students may write correct answers in different forms which makes grading very ha…
题目大意: 给定A,B两种字符串,问他们当中的长度大于k的公共子串的个数有多少个 这道题目本身理解不难,将两个字符串合并后求出它的后缀数组 然后利用后缀数组求解答案 这里一开始看题解说要用栈的思想,觉得很麻烦就不做了,后来在比赛中又遇到就后悔了,到今天看了很久才算看懂 首先建一个栈,从栈底到栈顶都保证是单调递增的 我们用一个tot记录当前栈中所有项和一个刚进入的子串匹配所能得到的总的子串的数目(当然前提是,当前进入的子串height值比栈顶还大,那么和栈中任意一个子串匹配都保持当前栈中记录的那时…
Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21084   Accepted: 7202 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…
题意: 析:利用单调栈,维护一个单调递增的栈,首先在最低的平台开始,每次向两边进行扩展,寻找两边最低的,然后不断更新宽度. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream>…
Boolean Expressions 首先声明此题后台可能极水(毕竟这种数据不好造!).昨天写了一天却总是找不到bug,讨论区各种数据都过了,甚至怀疑输入有问题,但看到gets也可以过,难道是思路错了? 题意:V表示ture,F表示false,然后有三种位运算符'!'.'&'.'|'.其中'!'的优先级最高,'|'的优先级最低.即优先级关系:! > & > | .给你一串包含这些运算符的表达式当然了还有括号,要你判断最终结果是VorF. 先说说我的思路吧:符号栈和数值栈肯定是…
原题目网址:http://poj.org/problem?id=1686 题目中文翻译: Description 数学教师懒得在考卷中给一个问题评分,因为这个问题中,学生会为所问的问题提出一个复杂的公式,但是学生可以用不同的形式写出正确的答案,这使得评分非常困难. 所以,教师需要计算机程序员的帮助,或许你可以提供帮助. 你应该编写一个程序来阅读不同的公式,并确定它们是否在算术上相同.   Input 输入的第一行包含一个整数N(1 <= N <= 20),即测试用例的数量. 在第一行之后,每个…
Description There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreove…
Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26549   Accepted: 8581 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal wi…
Description Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all real numbers. wi, hi for i = 1 . . . n are some elements in N, and w0 = 0.  Define set B = {< x, y > | x, y ∈ R and there exists an index i > 0 such…
这几天的知识学习比较多,因为时间不够了.加油吧,为了梦想. 这里写几条简单的单调栈作为题解记录,因为单调栈的用法很简单,可是想到并转化成用这个需要一些题目的积淀. 相关博客参见:https://blog.csdn.net/wubaizhe/article/details/70136174 POJ 3250 Bad Hair Day 题意与分析 题意是这样的:\(n\)个牛排成一列向右看,牛\(i\)能看到牛\(j\)的头顶,当且仅当牛\(j\)在牛\(i\)的右边并且牛\(i\)与牛\(j\)之…
题意 (Codeforces 548D) 对一个有$n$个数的数列,我们要求其连续$x(1\le x\le n)$(对于每个$x$,这样的连续group有若干个)的最小数的最大值. 分析 这是一道用了单调栈的题目,用的贼好.算是第一次应用吧. 我们定义$l_i$为左侧比第$i$个数小的数的下标的最大值(没有就是0):$r_i$就是右侧比第$i$个数小的数的下标的最小值(没有就是$n+1$).这样定义完后,我们会发现,$a_i$是$[a_{l_i+1},a_{r_i-1}]$的最小值,也就是siz…
n位密码,要用尽可能短的序列将n位密码的10n种状态的子串都包括,那么要尽量地重合. 题目已经说最短的是10n + n - 1,即每一个状态的后n-1位都和序列中后一个状态的前n-1位重合. 这题是经典的欧拉路径问题吧,用n位数字10n种状态来作为边,而用重合的n-1位数字表示点. 具体的建图,每个点都引出10条边(十进制),这10条边就代表着10个n位数,前n-1位的数就代表那个点,然后连向这个边代表数的后n-1位代表的点.. 比如n等于3的时候这么建图(假设密码是二进制,十进制太多了): 这…
layout: post title: 「kuangbin带你飞」专题十八 后缀数组 author: "luowentaoaa" catalog: true tags: - kuangbin - 字符串 - 后缀数组 传送门 倍增法 struct DA{ bool cmp(int *r,int a,int b,int l){ return r[a]==r[b]&&r[a+l]==r[b+l]; } int t1[maxn],t2[maxn],c[maxn]; int r…
再次面对像栈和队列这样的相当基础的数据结构的学习,应该从多个方面,多维度去学习. 首先,这两个数据结构都是比较常用的,在标准库中都有对应的结构能够直接使用,所以第一个阶段应该是先学习直接来使用,下一个阶段再去探究具体的实现,以及对基本结构的改造! C++标准库中的基本使用方法: 栈: #include<stack> 定义栈,以如下形式实现: stack<Type> s; 其中Type为数据类型(如 int,float,char等) 常用操作有: s.push(item);    /…