51nod 2478 -- 小b接水

https://www.51nod.com/Challenge/Problem.html#!#problemId=2478

思路:思维题,从高到低排序,两个高的中间可以盛水,如果有矮的在两个中间,就再减掉矮的所占的体积,。

例如435,4 5中间能装高度4的水,中间有个3,再减去3占的体积。

 

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const long long mod=1000000007;
const int maxn=50005;
int n,x,ans,l,r;
struct AA
{
    int h,x;
    bool operator<(const AA &aa)const{
        return h>aa.h;
    }
}pos[maxn];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&pos[i].h);
        pos[i].x=i;
    }
    sort(pos+1,pos+1+n);
    l=r=pos[1].x;
    for(int i=2;i<=n;i++)
    {
        if(pos[i].x>=l&&pos[i].x<=r) ans-=pos[i].h;
        else if(pos[i].x<l)
        {
            ans+=(l-1-pos[i].x)*pos[i].h;
            l=pos[i].x;
        }
        else
        {
            ans+=(pos[i].x-r-1)*pos[i].h;
            r=pos[i].x;
        }
    }
    printf("%d\n",ans);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值