POJ 3250 Bad Hair day analog monotonic stack

Source: Internet
Author: User

Bad Hair Day
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14989 Accepted: 4977

Description

Some of Farmer John ' s n cows (1≤ n ≤80,000) is has a bad hair day! Since Each cow was self-conscious about she messy hairstyle, FJ wants to count the number of other cows so can see the to P of other cows ' heads.

Each cow i have a specified height hi (1≤ hi ≤1,000,000,000) and is standing in a line of cows All facing east (to the right with our diagrams). Therefore, Cow i can see the tops of the heads of cows in front of her (namely cows i+1, i+2, a nd so on), for as long as these cows is strictly shorter than cow I.

Consider this example:

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 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 which is the height of cow I.

Output

Line 1: A single integer which is the sum of C1 through CN.

Sample Input

610374122

Sample Output

5


To tell a sequence, to ask for the number of small numbers behind each number and

#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include < algorithm> #include <cmath> #include <queue> #define N 100009typedef Long Long ll;using namespace Std;ll a[n ];int Main () {    int n;    while (~SCANF ("%d", &n))    {for        (int i=1;i<=n;i++)            scanf ("%i64d", &a[i]);        ll Ans=0;        int b[n];        B[n]=n;        for (int i =n-1;i>=1;i--)        {            int tt=i;            while (tt<n&&a[i]>a[tt+1]) tt=b[tt+1];//strictly monotonous, do not go to the equal sign            b[i]=tt;        } for        (int i=1;i<=n;i++)//        cout<<b[i]<< "";        for (int i=1;i<=n;i++)        {            ans+=b[i]-i;        }        printf ("%i64d\n", ans);    }    return 0;}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 3250 Bad Hair Day simulation monotonic stack

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.