Poj 3250 Bad Hair day monotonic stack __ Stack && queue

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.