P1901 emission problem solution [station]

Wwwwww unscrupulous propaganda about blog
Article list - nuclear fusion reactor core - Luo Gubo off


A signal column in descending order according to the stack height

For a new signal tower:

1

  • As shown, when he added to the stack,
    it will block until it is lower than the propagation of the tower
    , it would also lower than it receives a signal tower

    • So it will be lower than the top of the stack all the towers removed
      (because they can no longer spread to the other tower signal)
      at the same time, the new tower received energy
      plus deleted tower transmission of energy

2

  • As shown, the original height ratio for its high towers,
    from this new signal towers nearest, will receive the new signal tower

    • Therefore, at this time the column top of the stack,
      i.e., a signal from the new latest column, the column height higher than its
      received energy,
      coupled with the energy propagating new column
  • This new signal tower and then onto the stack

\ (O (n) \) after sweeping over complexity, all received energy ordering the column at
the output to a maximum value

attach The ugly Code:

#include<cstdio>
#include<algorithm>
#include<stack>
#define ll long long
using namespace std;
const int MARX = 1e6+10;
ll n;
ll h[MARX],v[MARX],w[MARX];
stack <int> s;
signed main()
{
    scanf("%lld",&n);
    for(ll i=1;i<=n;i++)
      {
        scanf("%lld%lld",&h[i],&v[i]); 
        while(!s.empty())// 将栈顶所有比它低的塔删除,
          {
            if(h[s.top()]>h[i]) break;//遇到比新塔高的 
            w[i]+=v[s.top()];// 加上 删掉的塔传播的能量 
            s.pop();
          }
        if(!s.empty()) w[s.top()]+=v[i];//加上新的塔传播的能量  
        s.push(i);//加入栈 
      }
    sort(w+1,w+n+1);//找到最大值 
    printf("%lld",w[n]);
}

Guess you like

Origin www.cnblogs.com/luckyblock/p/11456380.html