poj3658 模拟

14 篇文章 0 订阅
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 110000
#define ll long long
#define inf 1000000000000
ll ans[maxn],last[maxn],next[maxn],w[maxn],h[maxn];
int main ()
{
    int i,j,k,n;
    scanf("%d",&n);
    w[0]=0;
    h[0]=inf;
    w[n+1]=0;
    h[n+1]=inf;
    k=0;
    for (i=1;i<=n;i++)
    {
        scanf("%d%d",&w[i],&h[i]);
        last[i]=i-1;
        next[i-1]=i;
        next[i]=i+1;
        last[i+1]=i;
        if (h[i]<h[k])
        {
            k=i;
        }
    }
    ll now=0;
    for (j=1;j<=n;j++)
    {
        while ((h[last[k]]<h[k])||(h[next[k]]<h[k]))
        {
            if (h[last[k]]<h[k])
            {
                k=last[k];
            }
            else k=next[k];
        }
        ans[k]=now+w[k];
        last[next[k]]=last[k];
        next[last[k]]=next[k];
        if( h[last[k]]<h[next[k]] )
        {
            now=now+w[k]*(h[last[k]]-h[k]);
            w[last[k]]=w[last[k]]+w[k];
            k=last[k];
        }
        else
        {
            now=now+w[k]*(h[next[k]]-h[k]);
            w[next[k]]=w[next[k]]+w[k];
            k=next[k];
        }
    }
    for(i=1 ;i<= n ;i++)
        printf("%lld\n",ans[i]);
     return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值