题目链接:https://ac.nowcoder.com/acm/contest/984/A

链接:https://ac.nowcoder.com/acm/contest/984/A
来源:牛客网

Bad Hair Day
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

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.

输入描述:

Line 1: The number of cows, N.Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i.

输出描述:

Line 1: A single integer that is the sum of c1 through cN.
示例1

输入

复制

6
10
3
7
4
12
2

输出

复制

5

题目大意:输入N  有N个数 求每个数与它后面的数之间有多少个数  (总和)思路:利用栈求解,栈的用途还挺多的,具体看代码:
#include<iostream>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<map>
#include<queue>
#include<cstring>
using namespace std;
typedef long long LL;
const int maxn=1e9+5;
stack<int>s;
int main()
{int N,x;LL ans=0;cin>>N;for(int i=1;i<=N;i++){cin>>x;while(!s.empty()){int y=s.top();if(y<=x) s.pop();//如果当前的数比栈顶元素大 出栈else//否则这个数可以对栈中所有元素都贡献一个
            {ans+=s.size();break;}}s.push(x);}cout<<ans<<endl;return 0;
}

转载于:https://www.cnblogs.com/caijiaming/p/11157932.html

Bad Hair Day(求数组中元素和它后面离它最近元素之间的元素个数)相关推荐

  1. 动态规划系列---求数组中两个元素差的最大值

    题目 求数组中两个元素差的最大值(后面的元素减去前面的元素):对应实际生活中的股票买卖,找出一只股票走势里面可能的最大收益: 思路 类似于求数组连续和的最大值:  保存最大差值和最小值,遍历数组,如果 ...

  2. 分治法求数组中的最大最小元素

    #include<iostream> using namespace std; //分而治之法求数组中的最大最小元素 void maxmin(int i,int j,int A[],int ...

  3. JavaScript求数组中每个元素的个数,如数组str = [1,2,3,2,4,1,2]中元素2的个数为2。要求:使用对象知识来实现,输出结果为对象,对象包含数组元素和个数值。

    求数组中每个元素的个数,如数组str = [1,2,3,2,4,1,2]中元素2的个数为2. 要求:使用对象知识来实现,输出结果为对象,对象包含数组元素和个数值. <script>var ...

  4. 一题多解——求数组中每个元素出现的次数

    好久没更新博客了,写博客分享是个好习惯,发现坚持是比较难得的一件事情. 2021年第一更,就写一篇比较简单常用的算法入门题吧,主要是利用程序算法思想,求数组中每个元素出现的次数. 先看一下需求描述: ...

  5. 求数组中k个数的所有组合

    /*** 求数组中 k个元素序列 的所有组合* @param start* @param array* @param length* @param k* @param list* @param use ...

  6. 编程之美4:求数组中的最大值和最小值

    方法1:暴力方法 遍历一遍数组,比较2*N次求出最大值和最小值 方法2:改进方法 (破坏了原数组)             遍历一遍数组使得下标为偶数的元素较下标为奇数的元素大,再分别求出最大值和最小 ...

  7. (算法)求数组中数字组合(可多值组合)相加最接近目标数的组合(可能多个)

      今天没事,撸一道算法题   题目要求: 给出一个升序排序的可能重复值的数字数组和一个目标值其中目标值大于数组中最小数,求数组中数字组合(可多值组合)相加最接近目标数的组合(可能多个)不考虑空间复杂 ...

  8. 如何求数组中绝对值最小的数?

    """ 有一个升序排列的数组,数组中可能有正数,负数或0,求数组中元素的绝对值最小的数.例如,数组[-10,-5,-2,7,15,50],该数组中绝对值最小的数是2 &q ...

  9. 求数组中的最大值和最大值的索引

    前天将数据的一些遍历方法共享出来了,今天还是接着共享有关数组的吧. using System; using System.Collections.Generic; using System.Text; ...

  10. 求数组中最长递增子序列的长度

    题目:写一个时间复杂度尽可能低的程序,求一个一维数组(N个元素)中最长递增子序列的长度. 例:在序列[1, -1, 2, -3, 4, -5, 6, -7]中,其最长递增子序列的长度为4([1, 2, ...

最新文章

  1. 天冷了,大家如果有往年的不穿的衣服别扔,寄给需要的人好吗?
  2. openresty开发系列1--网关API架构及选型
  3. NYOJ_269_VF
  4. C# 实现HTTP不同方法的请求示例
  5. 2016上半年中国云存储排行榜:阿里云居榜首
  6. 【华为云技术分享】云小课 | 购买的数据盘在服务器看不到?磁盘初始化很重要!
  7. tensorflow实践笔记:屏蔽GPU版tensorflow的提示、分布式训练部署时的问题以及tensorboard的查看
  8. UBNT Bullet M2说明书
  9. 478.在圆内随机生成点
  10. Maix Bit(K210) 裸机开发教程(六)摄像头使用
  11. 535A: Tavas and Nafas
  12. [蓝牙] 1、蓝牙核心技术了解(蓝牙协议、架构、硬件和软件笔记)
  13. 【ubuntu拷贝目录】cp: omitting directory”错误的解释和解决办法
  14. android cs,《CS反恐精英》Android版 经典游戏再现
  15. 菜鸟窝-数据结构与算法之数组实现队列
  16. 记录一下Android开发中遇到的问题和解决方案
  17. 使用easyexcel进行excel的导入和导出(web)
  18. 【操作系统】多级索引、混合索引例题
  19. Tomcat异常Serveral ports(8005,8080,8009) required by Tomcat v8.0 Server at localhost are already in us
  20. MAC OS 配置ATOM 编写python

热门文章

  1. 浅析基本事实表的ETL处理
  2. 《设计模式——基于C#的工程化实现及扩展》
  3. 2022 智能语音技术最新进展与发展趋势
  4. 推荐系统技术演进趋势:排序篇
  5. windows10没有nfs服务_CentOS 7集群间实现NFS文件共享
  6. java final 函数_JAVA中Final的用法
  7. Star Schema完全参考手册学习笔记六
  8. springboot security
  9. 查看数据库表空间.md
  10. P3254 圆桌问题