Bad Hair Day-POJ3250(简单的入栈出栈)
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 specified height hi (1 ≤ hi ≤ 1,000,000,000) and is standing in a line of cows all facing east (to the right in our diagrams). Therefore, cow i can see the tops of the heads of cows in front of her (namely cows i+1, i+2, and so on), for as long as these cows are strictly shorter than cow i.
Consider this example:
=
= =
= - = Cows facing right -->
= = =
= - = = =
= = = = = =
1 2 3 4 5 6
Cow#1 can see the hairstyle of cows #2, 3, 4
Cow#2 can see no cow's hairstyle
Cow#3 can see the hairstyle of cow #4
Cow#4 can see no cow's hairstyle
Cow#5 can see the hairstyle of cow 6
Cow#6 can see no cows at all!
Let ci denote the number of cows whose hairstyle is visible from cow i; please compute the sum of c1 through cN.For this example, the desired is answer 3 + 0 + 1 + 0 + 1 + 0 = 5.
Input
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i.
Output
Sample Input
6
10
3
7
4
12
2
Sample Output
5 我本来用递归写的 现在发现好笨啊
简单的栈就可以
题目大意: 给你n个数,求每个数后面比他小的个数和 这个题是先把第一个a[0]入栈 再开始判断a[i]和入栈内的
如果栈顶的呢一个数比a[i]大 说明a[i]可以被栈内的每一个数看到然后就可以直接加上栈的长度。
然后再把a[i]入栈。
#include<stdio.h>
#include<stack>
#include<iostream>
#include<string.h>
using namespace std;
#define N 88000
stack<int >Q;
int a[N];
int main()
{
int n,i;
while(scanf("%d",&n)!=EOF)
{
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
Q.push(a[]);
long long ans=;
for(i=;i<=n;i++)
{
while(!Q.empty() && Q.top()<=a[i])
Q.pop();
ans+=Q.size();
Q.push(a[i]);
}
printf("%lld\n",ans);
}
return ;
}
Bad Hair Day-POJ3250(简单的入栈出栈)的更多相关文章
- bzoj 4034 [HAOI2015]树上操作 入栈出栈序+线段树 / 树剖 维护到根距离和
题目大意 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子树中所有点的点权都 ...
- php栈的定义及入栈出栈的实现 算法
转自:php栈的定义及入栈出栈的实现 栈是线性表的一种,他的特点是后入先出,可以这么理解,栈就像一个存东西的盒子,先放进去的在最底层,后放进去的在上层,因为上层的东西把底层的东西压住了,下层的想要出去 ...
- Python模拟入栈出栈操作
目标: 1.编写菜单,提示用户操作选项(push,pop,view,quit) 2.规则:定义列表,先入栈,后出栈,后入栈,先出栈 1.模拟入栈.出栈操作 >>> list1 = [ ...
- [置顶] 栈/入栈/出栈顺序(c语言)-linux
说明: 1.栈底为高地址,栈顶为低地址. 2.入栈顺序:从右到左. 解释1:栈在内存中的结构 [注:0x00 到 0x04之间间隔4个地址] 入栈:指针先指向0x10,从高地址向低地址方向填数值,最终 ...
- 5, java数据结构和算法: 栈 , 入栈, 出栈, 正序遍历,,逆序遍历
直接上代码: class ArrayStack{ //用数组模拟栈 int maxSize; int[] stack; int top = -1;//表示栈顶 public ArrayStack(in ...
- 对Viewcontroller在UINavigationController中入栈出栈的一点点理解
转载自:http://blog.csdn.net/intheair100/article/details/41119073 wait_record_arr 在viewdidload里面被alloc,如 ...
- mem之读操作调式总结(跟入栈出栈有关)
现象: 1.当case比较复杂的时候(含有for循环对mem进行读/写) 发现for循环时总是有汇编指令不执行跳过去了,(其实是汇编不熟和指令太多无法理智分析指令了). 事实是指令是对的,但执行错了( ...
- const以及入栈出栈
#include "stdafx.h"#include <iostream>using namespace std; class StringStack{ enum{s ...
- 面试 16:栈的压入压出队列(剑指 Offer 第 22 题)
我们今天继续来看看周五留下的习题: 面试题:输入两个整数序列,第一个序列表示栈的压入顺序,请判断二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如:压入序列为{1,2,3,4,5},那{ ...
随机推荐
- 5 月编程语言排行榜:Java第一,R跌出Top20
我们都知道,最近,TIOBE 发布了 5 月份编程语言排行榜.其中,前三名依然健稳不变,他们分别是 Java.C.C++,第四则为: Python ,第五则为 VB .NET. 下面两张图,我们可以看 ...
- 百度地图对https的支持
在使用百度地图时,如果直接使用其提供的js地址,在通过https的方式请求时,是不支持的 <script type="text/javascript" src="h ...
- Android Studio 导入新工程项目
1 导入之前先修改工程下相关文件 1.1 只需修改如下三个地方1.2 修改build.gradle文件 1.3 修改gradle/wrapper/gradle-wrapper.properties 1 ...
- json两层解析
public class Demo { public static void main(String[] args) { try { // 创建连接 服务器的连接地址 URL url = new UR ...
- R in action读书笔记(5)-第七章:基本统计分析
7.1描述性统计分析 > vars<-c("mpg","hp","wt") > head(mtcars[vars]) ...
- CSS——BFC
http://www.cnblogs.com/lhb25/p/inside-block-formatting-ontext.html 元素若不是bfc,那么内部浮动元素的高度不参与计算 元素若不是bf ...
- VS2015 update3 安装 asp.net core 失败
CMD 命令下执行: C:\DotNetCore\DotNetCore.1.0.0-VS2015Tools.Preview2.exe SKIP_VSU_CHECK=1
- OpenMP入门教程(二)
OpenMP API概述 OpenMP由三部分组成: 编译指令(19) 运行时库程序(32) 环境变量(9) 后来的API包含同样的三个组件,只是三者的数量都有所增加. 编译器指令 OpenMP编译器 ...
- POJ数据的输入输出格式
POJ在评阅习题时需要向程序提供输入数据,并获取程序的输出结果.因此提交的程序需按照每个习题具体的输入输出格式要求处理输入输出.有的时候,测评系统给出程序的评判结果是“数据错误”或“结果错误”,有可能 ...
- CAD参数绘制填充(网页版)
填充是CAD图纸中不可或缺的对象,在机械设计行业,常常需要将零部件剖开,以表现其内部的细节,而这些被剖开的截面会用填充来表示:在工程设计行业,一些特殊的材料或地形,也会用填充来表示. js中实现代码说 ...