2013年NOIP全国联赛提高组 1047 积木大赛

版权声明:转载请注明出处哦。 https://blog.csdn.net/Dch19990825/article/details/89844470

2013年NOIP全国联赛提高组 1047: 积木大赛

题目链接:http://129.211.20.246/problem.php?id=1047

思路:

​ 积木从左到右排列高度会形成一个似抛物线的形状,那么我们可以找到一个“沟” 在两个沟之间计算费用,其费用等于 其中最大高度减去左边的沟的高度。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll H[100005];
int main()
{
    ll n,ans,last,maxx;
    scanf("%lld",&n);
    for(ll i=1;i<=n;++i) scanf("%lld",H+i);
    last=0,maxx=0,ans=0;
    for(ll i=1;i<=n;++i)
    {
        if(i==n)
        {
            maxx=max(maxx,H[i]);//最后一下也结算费用
            ans+=maxx-last;
        }
        else if((H[i]<=H[i-1]&&H[i]<=H[i+1]))//这个地方是个沟,结算上次的费用
        {
            ans+=maxx-last;
            maxx=last=H[i];
        }
        else
            maxx=max(maxx,H[i]);
    }
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Dch19990825/article/details/89844470
今日推荐