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.
约翰有N头牛,他们正在过非主流头发节,因为每一头牛都清楚它的发型,FJ想去数数一头牛可以看到多少头别的牛的发型?
每头牛i都有一个确定的身高,这些牛站成一条线面向东部(直线上的右部),因此,牛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.
输入
6 10 3 7 4 12 2
输出
5
由于统计一头牛看见多少头牛实在是有点困难(对于维护一个单调递减的单调栈来说),并且复杂度相当的高(变动一个,已经计算出来的结果都得变)。所以可以换个想法,A牛看见三头牛的同时意味着三头牛被A看见,而计算“被看见”只需要在单调栈里把留存的单调递减的部分(如同题意,高的看矮的)统计上即可,注意要在把当前研究对象压栈之前更新答案,否则会出现自己看见自己的情况。
#include<pch.h>
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
#include <map>
#include <algorithm>
#include <stack>
#include <iomanip>
#include <cstring>
#include <cmath>
#define DETERMINATION main
#define lldin(a) scanf_s("%lld", &a)
#define println(a) printf("%lld\n", a)
#define reset(a, b) memset(a, b, sizeof(a))
const int INF = 0x3f3f3f3f;
using namespace std;
const double PI = acos(-1);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int mod = 1000000007;
const int tool_const = 19991126;
const int tool_const2 = 2000;
inline ll lldcin()
{
ll tmp = 0, si = 1;
char c;
c = getchar();
while (c > '9' || c < '0')
{
if (c == '-')
si = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
{
tmp = tmp * 10 + c - '0';
c = getchar();
}
return si * tmp;
}
///Untersee Boot IXD2(1942)
/**Although there will be many obstructs ahead,
the desire for victory still fills you with determination..**/
/**Last Remote**/
stack<ll>stk;
ll a[760000];
int DETERMINATION()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
ll n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
ll ans = 0;
for (int i = 1; i <= n; i++)
{
while (stk.size() && stk.top() < a[i])//根据定义驱逐栈内成员
stk.pop();
ans += stk.size();
stk.push(a[i]);
}
cout << ans << endl;
return 0;
}