题解:P15034 [UOI 2021 II Stage] 奇迹之地
这也是绿吗(
思路
第一点要求每棵树必须保留
然后看到第二点,在进行操作时,会在
所以在到选到了
否则,在可以选择时将
为了判断上述情况是否发生,可以维护当前已有树的最大值
因此,若不
时间复杂度
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+10;
int n,a,mx,ans;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a;
if(mx<a-1){//如果前面没有比当前数量-1更大的树,则可以对这棵树进行操作,修改这棵树的高度。
ans+=a-1;
a=1;
}
mx=max(mx,a);//维护硬币数的最大值。
}
cout<<ans;
return 0;
}