[BZOJ1657] [Usaco2006 Mar] Mooo Cow's song (monotonous stack)

Source: Internet
Author: User

Description

Farmer John's N (1 <= n <= 50,000) cows is standing in a very straight row and mooing. Each cow have a unique height h in the range 1..2,000,000,000 nanometers (FJ really are a stickler for precision). Each cow Moos at some volume V in the range 1..10,000. This "moo" travels across the row of cows in both directions (except for the end cows, obviously). Curiously, it is heard only by the closest cow a direction whose height is strictly larger than that of the mooing C ow (so each moo would be heard by 0, 1 or 2 other cows, depending on not whether or taller cows exist to the mooing cow ' s R Ight or left). The total Moo volume heard by given cow are the sum of all the Moo volumes V for all cows whose mooing reaches the cow. Since some (presumably taller) cows might be subjected to a very large moo volume, FJ wants to buy earmuffs for the cow wh OSE hearing is the most threatened. Please compute the loudest moo volume heard by any cow.

Farmer John's N (1<=n<=50,000) cows stood neatly in a row of "howling". Each cow has a definite height of H (1<=h<=2000000000), called the volume of V (1<=v<=10000). Each cow's cry spreads to both ends, but in each direction it is only heard by the nearest cow whose height is strictly greater than it, so each call is only heard by the 0,1,2 cow (depending on whether there is a cow higher on either side). The total volume that a cow hears is the sum of all the volume it hears. Since some cows have suffered a huge volume, Farmer John is going to buy an ear mask for the most maimed cows, and ask you to help him calculate the maximum total volume.

Input

* Line 1: A single integer, N.

* Lines 2..n+1:line i+1 contains II space-separated integers, H and V, for the cow standing at location I.

Line 1th: A positive integer n.

2nd to n+1: Each line consists of 2 integers separated by spaces representing the height of the cow standing in the first position of the team and the volume of her singing.

Output

* Line 1:the Loudest moo volume heard by any single cow.

The highest total volume that a cow in a team can hear.

Sample Input3
4 2
3 5
6 10

INPUT DETAILS:
Three cows:the first one has a height 4 and Moos with Volume 2, etc.
Sample Output7
HINT

The 3rd cow in the team could hear the song of the 1th and 2nd cows, so the total volume she could hear was 2+5=7. Although she sang at the volume of 10, but no cows can hear her singing.

Source

Silver

Solution

Nothing to say, monotonous stack maintenance is not heard at present, before and after each maintenance on the line

1#include <bits/stdc++.h>2 using namespacestd;3 inth[50005], v[50005], w[50005], sta[50005], top;4  5 voidPushintx)6 {7      while(Top && H[x] >H[sta[top]])8W[X] + = v[sta[top--]];9Sta[++top] =x;Ten } One   A intMain () - { -     intN, ans =0; thescanf"%d", &n); -      for(inti =1; I <= N; i++) -scanf"%d%d", H + I, V +i); -      for(inti =1; I <= N; i++) + push (i); -top =0; +      for(inti = n; I i--) A push (i); at      for(inti =1; I <= N; i++) -Ans =Max (ans, w[i]); -printf"%d\n", ans); -     return 0; -}
View Code

[BZOJ1657] [Usaco2006 Mar] Mooo Cow's song (monotonous 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.