土地恢复
解题思路
诺,原题是这个,只不过加强了数据。
话说有人用 O ( n ) O(n) O(n) 的方法来做,这肯定是正解,不像我这个菜鸡只能敲暴力。
code
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,a[100010],ans;
struct abc{
int x,s;
}b[100010];
bool cmp(abc a,abc b)
{
if(a.s!=b.s)
return a.s<b.s;
return a.x<b.x;
}
void fz(int l,int r,int last)
{
if(l==r)
{
ans+=a[l];
a[l]=0;
return;
}
int t=0x3f3f3f3f;
for(int i=l;i<=r;i++)
t=min(t,a[i]);
ans+=t;
for(int i=l;i<=r;i++)
a[i]-=t;
for(int i=l;i<=r;i++)
if(a[i]==0)
{
if(i!=l)
fz(l,i-1,last);
l=i+1;
}
if(a[r]!=0)
fz(l,r,last);
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
fz(1,n,1);
cout<<ans<<endl;
}