链接: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';
}
}
}

最新文章

  1. Java多线程系列--“JUC原子类”01之 框架
  2. Linux压缩那些事儿
  3. VIM default configuration
  4. css3 翻转和旋转的差别
  5. (转)mahout中k-means例子的运行
  6. Windows下安装Python扩展模块提示Unable to find vcvarsall.bat的问题
  7. 记一次lvs-tunnel模式的故障分析(7)
  8. CDlinux系统破解无线wifi
  9. Java_myBatis_全局配置文件
  10. cmd下 mysql操作命令大全详解
  11. IOC入门
  12. 批量生成protoBuf到cs文件
  13. 多线程下载文件,ftp文件服务器
  14. zabbix的日常监控-分布式监控(十)
  15. Journal entry of the thirteenth chapter to chapter seventeenth(第十三章和十七章阅读与疑问)
  16. linux配置java环境
  17. WeifenLuo.WinFormsUI.Docking.dll的用法
  18. 20155212 mybash的实现
  19. ES6 标签模板
  20. spring-cloud - 基础环境搭建

热门文章

  1. python 不灭
  2. send()/ recv() 和 write()/ read()
  3. SQL Server Availability Group Failover 测试
  4. jQuery file upload callback options
  5. spring boot官方配置
  6. 【flask_sqlalchemy】模糊查询
  7. 2018 icpc 沈阳
  8. 【工具安装】BurpSuite 安装教程
  9. xshell 缺少mfc110u.dll
  10. 【MM系列】SAP 在SAP中更改基本计量单位