Poj 3250 bad hair day

Source: Internet
Author: User

Description

Some Of Farmer John'sNCows (1 ≤N≤ 80,000) are having a bad hair day! Since each cow is self-conscious about hermessy hairstyle, FJ wants to count the number of other cows that can see thetop of other cows 'heads.

Each cowIHas a specified heightHi(1 ≤
Hi≤ 1,000,000,000) and is standing in a line of cowsall facing east (to the right in our diagrams). Therefore, cow
ICan seethe 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!

LetCiDenote the number of cowswhose hairstyle is visible from cow
I; Please compute the sumC1 throughCN. 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: LineI+ 1 contains a single integer that is the height ofcow
I.

Output

Line 1: A single integer thatis The sumC1 through
CN.

Sample Input

6

10

3

7

4

12

2

Sample output

5

 

Topic Introduction: A group of cows lined up and headed to the right. Ask each ox how many cows have their heads in front (that is, how many cows are shorter than themselves in Front ). Then calculate the total number.

Method: Double-end queue. The subscript of the array that stores cattle in the queue. The height is sorted in descending order. When the processed data is greater than the height of the cow corresponding to the subscript at the end of the queue, add the value of the team end counter to the total number, use count to save the value of the team end counter at this time, and then delete the team end. At the same time, add (count + 1) to the counter at the end of the deleted team ). After data processing, perform similar operations from the end of the team.

For example, if the queue is 10, 3, processing 7, 3 <7, 3 cannot see any cow, namely <3, 0>, sum + = 0, delete 3, and Add 1 to the counter of 10, 10 indicates that one ox (<10, 1>) and 10> 7 can be seen. Continue to process the subsequent data. At this time, the queue is 10, 7. Process 4,4 less than 7 and continue. Processing> cannot see any ox, that is, <>, sum + = 0, delete 4, and Add 1 to the counter of 7, that is, <>. Continue to compare, 7 <12, sum + = 1,

Add 2 to the counter of 10, that is, <10, 3>. 10 <12, sum + = 3. If there is no data before 10, delete 10. Next, Process <12 without any operation. Then the data processing is complete. Start from the end of the team and delete 2, <2., 0>, sum + = 0. Then add 1 to 12, that is, <12, 1>, sum + = 1, and delete 12. The queue is empty and complete.

Limited language capabilities ......

 

 

#include<iostream>#include<deque>using namespace std;struct node{long long int high;int count;}cow[80001];int main(){int n, i;scanf("%d",&n);for(i = 0;i<n;i++){scanf("%lld",&cow[i].high);}deque<int> q;q.clear();long long sum = 0;for(i = 0;i<n;i++){int count = 0;while(!q.empty()&&cow[i].high>=cow[q.back()].high){cow[q.back()].count += count;sum += cow[q.back()].count;count = cow[q.back()].count + 1;q.pop_back();}if(!q.empty()){cow[q.back()].count += count;}q.push_back(i);}int count = 0;while(!q.empty()){cow[q.back()].count += count;sum += cow[q.back()].count;count = cow[q.back()].count + 1;q.pop_back();}printf("%lld\n",sum);system("pause");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.