题目描述

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<cstdio>
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
#define LL long long
char buf[1<<21],*p1=buf,*p2=buf;
const int MAXN = 80001;
inline LL read() {char c = getchar(); LL x = 0, f = 1;while(c < '0' || c > '9') {if(c == '-') f = -1;c = getchar();}while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = getchar();}return x * f;
}
LL a[MAXN];
int  Ans[MAXN], s[MAXN];
int N, top = 0;
int main() {N = read();for(int i = 1; i <= N; i++) a[i] = read();s[0] = N + 1;LL ans = 0;for(int i = N; i >= 1; i--)    {while(top > 0 && a[i] > a[ s[top] ]) top--;ans += (s[top] - i - 1);s[++top] = i;}printf("%lld", ans);return 0;
}

洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day(单调栈)相关推荐

  1. 洛谷 P2867 [USACO06NOV]大广场Big Square

    P2867 [USACO06NOV]大广场Big Square 题目描述 Farmer John's cows have entered into a competition with Farmer ...

  2. 洛谷P1879 [USACO06NOV]玉米田Corn Fields【状压dp】

    P1879 [USACO06NOV]玉米田Corn Fields 时间限制 1.00s 内存限制 125.00MB 题目描述 Farmer John has purchased a lush new ...

  3. 洛谷P1879 [USACO06NOV]玉米田Corn Fields

    P1879 [USACO06NOV]玉米田Corn Fields 题目描述 Farmer John has purchased a lush new rectangular pasture compo ...

  4. 洛谷 P1879 [USACO06NOV]玉米田Corn Fields

    题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...

  5. 洛谷P1198 [JSOI2008]最大数

    P1198 [JSOI2008]最大数 267通过 1.2K提交 题目提供者该用户不存在 标签线段树各省省选 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 WA80的戳这QwQ BZOJ都 ...

  6. ⌈洛谷1505⌋⌈BZOJ2157⌋⌈国家集训队⌋旅游【树链剖分】

    题目链接 [洛谷] [BZOJ] 题目描述 Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T ...

  7. 2019.6.7 一场搜索专题的考试【including 洛谷·血色先锋队,入门OJ·兴建高铁,珠光宝气阁

    这次分数还好.但全是搜索题还没上200就有点打击人了--[本狸才177QAQ 血色先锋队/血色敢死队 传送门:洛谷P1332 & 入门OJ P2259 Description 邪魔天国领主复活 ...

  8. 洛谷-题解 P2672 【推销员】

    独门思路!链表加优先队列! 这题一望,贪心是跑不掉了,但是我贪心并不好,所以想到了一个复杂一些但思路更保稳的做法 思路: 1 因为是离线操作,所以我们可以倒着求,先求x=n的情况,因为那样直接就知道了 ...

  9. 洛谷 P1142 轰炸

    洛谷 P1142 轰炸 题目描述 "我该怎么办?"飞行员klux向你求助. 事实上,klux面对的是一个很简单的问题,但是他实在太菜了. klux要想轰炸某个区域内的一些地方,它们 ...

最新文章

  1. [置顶] 安卓高手之路之 WindowManager
  2. python开发的优秀界面-:Python做推荐系统,界面用什么实现
  3. 获取 HTML5 网页设计灵感的10个网站推荐
  4. oracle约束 关闭,Oracle约束管理脚本
  5. Python处理CSV大文件特定行数据
  6. Oracle Goldengate 安装配置
  7. 智能优化算法:适应度相关优化算法 - 附代码
  8. Servlet期末复习笔记
  9. android高通camera驱动调试,高通Camera模块驱动指南资料
  10. 《当程序员的那些狗日日子》三
  11. 2008服务器系统开启ftp,2008服务器开启ftp服务
  12. 方面和服务,差别大吗?
  13. 前端:LayUi监听表格单元格,编辑后恢复原数据
  14. java机顶盒_Java技术在数字电视机顶盒中的应用
  15. c#.net command 命令详细分析
  16. matlab化石墨烯,基于MATLAB的石墨烯场效应晶体管电学特性研究
  17. 反思:项目开发中的语言沟通与文档沟通
  18. dual,rowid,rownum
  19. 《中国聚合支付行业发展报告2018》发布 深度分析未来八大趋势
  20. 男朋友是一名程序员,他都好久没有交作业了

热门文章

  1. 全球及中国皮肤晒黑喷雾行业销售模式及动态盈利分析报告2021年版
  2. openssl与cryptoAPI交互AES加密解密
  3. WINSERVER 2008 CA 证书有效期修改
  4. Python学习日记(六) 浅深copy
  5. C# 往excel出力数据
  6. django 的 一对多的关系
  7. 新概念英语(1-61)A bad cold
  8. (3)WebApi客户端调用
  9. device eth0 does not seem to be present, delaying initialization
  10. MyBatis 多参数传递