Topic Link: Poj 3250 bad Hair Day
Bad Hair Day
Time limit:2000ms Memory limit:65536k
Total submissions:16700 accepted:5621
Description
Some of farmer John ' s N cows (1≤n≤80,000) are have a bad hair day! Since Each cow are self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can P of other cows ' heads.
Each cow i has a specified height hi (1≤hi≤1,000,000,000) and are standing in a line of cows all facing Ght in our diagrams). Therefore, cow I can-the tops of the heads of cows in front to her (namely cows i+1, i+2, and "), for as long as T Hese cows are strictly shorter than I.
Consider this example:
=
= =
=-= cows facing right–>
= = =
= - = = =
= = = = = =
1 2 3 4 5 6
Cow#1 can-hairstyle of cows #2, 3, 4
Cow#2 can no Cow ' s hairstyle
Cow#3 can-hairstyle of Cow #4
Cow#4 can no Cow ' s hairstyle
Cow#5 can-hairstyle of Cow 6
Cow#6 can no cows at all!
Let CI denote the number of cows whose hairstyle are 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.
Input
Line 1:the number of cows, N.
Lines 2..n+1:line i+1 contains a single integer this is the height of cow i.
Output
Line 1: "A single integer" Is the sum of C1 through CN.
Sample Input
6
10
3
7
4
12
2
Sample Output
5
Given the height of n cows, let D (i) be the total number of other cows that can be seen (see to the right) by the first cow. Beg ∑ni=1d[i] \sum_{i=1}^nd[i]
Thought: To the contrary, I think the cow can be seen by the number of cows on the left. Maintain a monotonous stack.
AC Code:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#define The CLR (A, B) memset (A, (b), sizeof (a))
using namespace std;
typedef long long LL;
const int MAXN = 1E4 +10;
const int INF = 0X3F3F3F3F;
int stack[80000+1];
int main ()
{
int N;
while (scanf ("%d", &n)!= EOF) {
LL-ans = 0; int top = 0;
for (int i = 1; I <= N; i++) {
int A; scanf ("%d", &a);
while (top && stack[top-1] <= a) {
top--;
}
Ans + + top;
stack[top++] = A;
}
printf ("%lld\n", ans);
}
return 0;
}