洛谷 P2866 [USACO06NOV]糟糕的一天Bad Hair Day 牛客假日团队赛5 A (单调栈)
链接:https://ac.nowcoder.com/acm/contest/984/A
来源:牛客网
题目描述
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
题意:
思路:
单调栈模版题
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
ll a[maxn];
// int st[maxn];
stack<ll> st;
int main()
{
//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
//freopen("D:\\common_text\code_stream\\out.txt","w",stdout);
gbtb;
cin>>n;
repd(i,1,n)
{
cin>>a[i];
}
ll ans=0ll;
repd(i,1,n)
{
while(!st.empty()&&st.top()<=a[i])
{
st.pop();
}
st.push(a[i]);
ans+=1ll*(st.size()-1);
}
cout<<ans<<endl;
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
洛谷 P2866 [USACO06NOV]糟糕的一天Bad Hair Day 牛客假日团队赛5 A (单调栈)的更多相关文章
- 洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day
P2866 [USACO06NOV]糟糕的一天Bad Hair Day 75通过 153提交 题目提供者洛谷OnlineJudge 标签USACO2006云端 难度普及/提高- 时空限制1s / 12 ...
- 洛谷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 ...
- 洛谷 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 ...
- 洛谷——P2866 [USACO06NOV]糟糕的一天Bad Hair Day
https://www.luogu.org/problem/show?pid=2866 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are h ...
- 单调栈 && 洛谷 P2866 [USACO06NOV]糟糕的一天Bad Hair Day(单调栈)
传送门 这是一道典型的单调栈. 题意理解 先来理解一下题意(原文翻译得有点问题). 其实就是求对于序列中的每一个数i,求出i到它右边第一个大于i的数之间的数字个数c[i].最后求出和. 首先可以暴力求 ...
- 洛谷 2866 [USACO06NOV]糟糕的一天Bad Hair Day
[题意概述] 给出一个长度为n的序列a,求有多少对[i,j]满足i<j且a[i]>max(a[i+1],a[i+2],...,a[j]). [题解] 单调栈. 倒着处理序列的元素,维护一个 ...
- 洛谷P2866 [USACO06NOV]Bad Hair Day S (单调栈)
看到这道题很容易想到单调栈,但我一开始想的是从后往前扫,但发现会有问题(因为这样会对后面牛的答案造成影响),所以这时我们要及时换一个思路,从前往后扫. 维护一个单调递减的栈,插入h[i]时,小等于它的 ...
- P2866 [USACO06NOV]糟糕的一天Bad Hair Day--单调栈
P2866 [USACO06NOV]糟糕的一天Bad Hair Day 题意翻译 农夫约翰有N (N \leq 80000)N(N≤80000)头奶牛正在过乱头发节.每一头牛都站在同一排面朝东方,而且 ...
- bzoj1660 / P2866 [USACO06NOV]糟糕的一天Bad Hair Day
P2866 [USACO06NOV]糟糕的一天Bad Hair Day 奶牛题里好多单调栈..... 维护一个单调递减栈,存每只牛的高度和位置,顺便统计一下答案. #include<iostre ...
随机推荐
- C#调用windows API实现 smallpdf客户端程序进行批量压缩
一.背景 Smallpdf 网站针对PDF文件提供了非常齐全的功能:PDF 与 Word.PPT.Excel.JPG 的相互转化.PDF 的压缩.编辑.合并.分割.解密.加密等功能,用户无需注册即可免 ...
- 如何在maven项目中引用领一个项目
1 有两个项目 maven01 和maven 02,想在maven 02中引用maven01的方法,该如何操作呢 maven01中Factory类中的方法 public class Factory ...
- GitHub - 解决 GitHub Page 404
带有下划线的文件报 404 解决:在仓库文件夹根目录添加.nojekyll文件 参见: Bypassing Jekyll on GitHub Pages - The GitHub Blog How t ...
- 搜索引擎、邮件营销、微信营销之营销方式大PK
经常有朋友问到这个问题,关于搜索引擎.邮件营销.微信营销三种网络营销方式的优劣势,用哪种营销方式比较好.下面我们跟随Focussend小编来看看这几种方式的优劣势. 搜索引擎 优势:1.传播速度快:2 ...
- jest 提示 Unexpected identifier 的解决方案
概述 今天在玩 jest 的时候,发现用 import 就会报 Unexpected identifier 的错误.查了很久的资料,最后终于解决了. 参考资料:Jest tests can't pro ...
- HTML——超级链接 表格 框架
- cocos2dx基础篇(9) 滑块控件CCControlSlider
[3.x] (1)去掉 “CC” (2)对象类 CCObject 改为 Ref (3)CCControlEvent 改为强枚举 Control::EventType (4)CCControlEvent ...
- 【转载】PHP中foreach的用法
http://www.php.cn/php-weizijiaocheng-399438.html 很好用的PHP中foreach的用法详解,收藏!
- IP子网的划分
一.划分子网的具体步骤 已知192.168.1.0/24,划分8个子网(一个ip划分8个子网,即网络位不再是24位)1100 0000.1010 1000.0000 0001.0000 00001 . ...
- Python_ONLINE_习题集_02 函数封装
2.1 封装函数实现如下要求 例如:输入2,5 则求:2 + 22+222 + 2222+22222的和 参考答案: https://www.bilibili.com/read/cv4185619 d ...