洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day(单调栈)
题目描述
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.
输入输出样例
6 10 3 7 4 12 2
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(单调栈)相关推荐
- 洛谷 P2867 [USACO06NOV]大广场Big Square
P2867 [USACO06NOV]大广场Big Square 题目描述 Farmer John's cows have entered into a competition with Farmer ...
- 洛谷P1879 [USACO06NOV]玉米田Corn Fields【状压dp】
P1879 [USACO06NOV]玉米田Corn Fields 时间限制 1.00s 内存限制 125.00MB 题目描述 Farmer John has purchased a lush new ...
- 洛谷P1879 [USACO06NOV]玉米田Corn Fields
P1879 [USACO06NOV]玉米田Corn Fields 题目描述 Farmer John has purchased a lush new rectangular pasture compo ...
- 洛谷 P1879 [USACO06NOV]玉米田Corn Fields
题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...
- 洛谷P1198 [JSOI2008]最大数
P1198 [JSOI2008]最大数 267通过 1.2K提交 题目提供者该用户不存在 标签线段树各省省选 难度提高+/省选- 提交该题 讨论 题解 记录 最新讨论 WA80的戳这QwQ BZOJ都 ...
- ⌈洛谷1505⌋⌈BZOJ2157⌋⌈国家集训队⌋旅游【树链剖分】
题目链接 [洛谷] [BZOJ] 题目描述 Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但又为了节约成本,T ...
- 2019.6.7 一场搜索专题的考试【including 洛谷·血色先锋队,入门OJ·兴建高铁,珠光宝气阁
这次分数还好.但全是搜索题还没上200就有点打击人了--[本狸才177QAQ 血色先锋队/血色敢死队 传送门:洛谷P1332 & 入门OJ P2259 Description 邪魔天国领主复活 ...
- 洛谷-题解 P2672 【推销员】
独门思路!链表加优先队列! 这题一望,贪心是跑不掉了,但是我贪心并不好,所以想到了一个复杂一些但思路更保稳的做法 思路: 1 因为是离线操作,所以我们可以倒着求,先求x=n的情况,因为那样直接就知道了 ...
- 洛谷 P1142 轰炸
洛谷 P1142 轰炸 题目描述 "我该怎么办?"飞行员klux向你求助. 事实上,klux面对的是一个很简单的问题,但是他实在太菜了. klux要想轰炸某个区域内的一些地方,它们 ...
最新文章
- [置顶] 安卓高手之路之 WindowManager
- python开发的优秀界面-:Python做推荐系统,界面用什么实现
- 获取 HTML5 网页设计灵感的10个网站推荐
- oracle约束 关闭,Oracle约束管理脚本
- Python处理CSV大文件特定行数据
- Oracle Goldengate 安装配置
- 智能优化算法:适应度相关优化算法 - 附代码
- Servlet期末复习笔记
- android高通camera驱动调试,高通Camera模块驱动指南资料
- 《当程序员的那些狗日日子》三
- 2008服务器系统开启ftp,2008服务器开启ftp服务
- 方面和服务,差别大吗?
- 前端:LayUi监听表格单元格,编辑后恢复原数据
- java机顶盒_Java技术在数字电视机顶盒中的应用
- c#.net command 命令详细分析
- matlab化石墨烯,基于MATLAB的石墨烯场效应晶体管电学特性研究
- 反思:项目开发中的语言沟通与文档沟通
- dual,rowid,rownum
- 《中国聚合支付行业发展报告2018》发布 深度分析未来八大趋势
- 男朋友是一名程序员,他都好久没有交作业了