day2的第一题,依旧不怎么难,中间利用了差分数列的原理,设一新数组d,其中有n+1项,d[i]=a[i+1]-a[i](令a[0]与a[n+1]都为0),其时间复杂复杂度为O
(n),代码如下:
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int a[100005],b[100005],n,ans; int main() { freopen("block.in","r",stdin); freopen("block.out","w",stdout); cin>>n; for (int i=1;i<=n;i++) { scanf("%d",&a[i]); } for (int i=0;i<=n;i++) { b[i]=a[i+1]-a[i]; } for (int i=0;i<=n;i++) ans+=max(0,b[i]); cout<<ans; return 0; }
清清正正射命丸文是也~