https://www.luogu.org/problem/show?pid=2866

题目描述

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.

农民约翰的某N(1 < N < 80000)头奶牛正在过乱头发节!由于每头牛都意识到自己凌乱不堪 的发型,约翰希望统计出能够看到其他牛的头发的牛的数量.

每一头牛i有一个高度所有N头牛面向东方排成一排,牛N在最前面,而 牛1在最后面.第i头牛可以看到她前面的那些牛的头,只要那些牛的高度严格小于她的高度,而且 中间没有比hi高或相等的奶牛阻隔.

让N表示第i头牛可以看到发型的牛的数量;请输出Ci的总和

输入输出格式

输入格式:

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
输出样例#1:

5
 #include <algorithm>
#include <iostream>
#define maxn 1000005 using namespace std; long long n,ans,top,x;
long long stack[maxn]; int main()
{
cin>>n;
for(int i=;i<=n;i++)
{
cin>>x;
while(top&&stack[top]<x)
top--;
ans+=top;
stack[++top]=x;
}
cout<<ans;
return ;
}

熟悉栈

最新文章

  1. spring MVC入门教程
  2. (原创)RS232串口信号定义
  3. session_id 恢复 session的内容
  4. [转]jqGrid 属性、事件全集
  5. iOS之07-三大特性之多态 + NSString类
  6. ADIv5.2
  7. iOS - UINavigationController
  8. uniq linux下去除重复行命令
  9. 【BZOJ 1191】 [Apio2010]特别行动队 (斜率优化)
  10. 偷偷mark下一个
  11. Websocket 与代理服务器如何交互? How HTML5 Web Sockets Interact With Proxy Servers
  12. IOS设备上网页中的页面滚动效果模拟
  13. Redis集群介绍
  14. Django_生产环境静态文件配置
  15. .NET Core微服务之基于Steeltoe使用Zipkin实现分布式追踪
  16. 【视频】使用ASP.NET Core开发GraphQL服务
  17. IView组件化之部署及按钮学习
  18. [转] Snapshotting with libvirt for qcow2 images
  19. 关于UNITY学习,给新生建议
  20. IIS回收后首次访问慢问题

热门文章

  1. MYSQL5.7 忘记ROOT密码/初始化ROOT密码
  2. GIS在石油行业中的应用
  3. codeforces_1075_C. The Tower is Going Home
  4. Android(java)学习笔记201:JNI之helloword案例(利用NDK工具)
  5. Java入门第38课——猜字母游戏之设计程序结构
  6. laravel学习:php写一个简单的ioc服务管理容器
  7. ALTER SEQUENCE - 更改一个序列生成器的定义
  8. Flex 布局 (两个div居中自适应 宽度变小变一列,宽度够就是两列)
  9. vue+webpack静态资源路径引用
  10. git 支持tree命令