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);
}