Los solution to a problem valley station transmitter P1901

P1901 transmitting station

Title Description

Somewhere there are N energy emitting stations arranged in a row, each transmitting station i does not have the same height as the Hi, and to the sides (the ends of course, only to the side) while transmitting energy in the energy value Vi, and the emitted energy is only recently and on both sides of the transmitting station is higher than its reception.

Obviously, each of the energy sent by transmitting station is likely to be accepted by 0 or 1 or 2 other transmitting station, in particular for safety, the sum of the energy of each received transmitting station is what we are concerned. Because a lot of data, you now only need to receive up to help calculate the energy transmitting station received energy is.

Input Format

Line 1: an integer N;

2 to line N + 1: i + 1-row two integers Hi and Vi, i represents a highly personal transmitting station and a transmission energy value.

Output Format

Only the output line, represents the energy of the most energy value of the received transmitting station is received, the answer is no more than longint.

Sample input and output

Input # 1

3
4 2
3 5
6 10

Output # 1

7

Description / Tips

For 40% of the data, 1 <= N <= 5000; 1 <= Hi <= 100000; 1 <= Vi <= 10000;

For 70% of the data, 1 <= N <= 100000; 1 <= Hi <= 2,000,000,000; 1 <= Vi <= 10000;

To 100% of the data, 1 <= N <= 1000000; 1 <= Hi <= 2,000,000,000; 1 <= Vi <= 10000.

[Thinking]

Pallet monotone sub title
(the signal energy is below the mean, said transmitted signal feeling more interesting than the emission energy)

[Title] effect

Many of signal tower
tower may transmit a signal
a signal transmitted only on both sides of the first column than he be able to receive signals

Why use a monotone [stack]

Monotonous stack is used to find a point on both sides of the first position is less than or greater than the point of
this is obviously right
because you're in that this number and compare the top of the stack
(assuming that the current find both sides of the first above this point
and Like the subject requirements)
If the number is greater than the top of the stack
that it is clear that the right of the first stack is greater than the number found is his right
and then if the number is less than the top of the stack
that it is clear that this number is left of the first number is greater than his also find the bars are
so monotonous that can be used on both sides of the stack of seeking a greater than / less than the number of positions

[Last] ideas

Stack sides monotonous find this number is greater than the first position
in the stack monotone monotonous process
above said stack if the number is greater than
that of the position signal received from this column can be capable of adding a signal emitted by the stack
If this number is less than the top of the stack
signal towers that this position can be sent that signal tower was where the top of the stack receive
the signal tower where the top of the stack can be received
on this number plus the tower where the issue can signal strength
for the last enumerated once every telephone pole signal received
comparison output to the maximum value of the okoj

[Complete code]

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
const int Max = 1000006;
struct node
{
    int hao;
    int h;
    int v;
    int w;
}a[Max];
stack<int>s;
int main()
{
    int n;
    cin >> n;
    for(register int i = 1;i <= n;++ i)
        cin >> a[i].h >> a[i].v,a[i].hao = i;
    for(register int i = 1;i <= n;++ i)
    {
        while(!s.empty() && a[s.top()].h < a[i].h)
        {
            a[i].w += a[s.top()].v;
            s.pop();
        }
        if(!s.empty())
        a[s.top()].w += a[i].v;
        s.push(i);
    }
    int M = 0;
    for(register int i = 1;i <= n;++ i)
        M = max(M,a[i].w);
    cout << M << endl;
    return 0; 
}

Guess you like

Origin www.cnblogs.com/acioi/p/11681307.html