和NOIp2013 积木大赛一模一样
我在堆一格的时候,我把它尽量地往右去延伸
于是如果对于一个i,a[i-1]<a[i],那i在之前一定只堆过a[i-1]那么多,所以要再堆a[i]-a[i-1]
如果a[i-1]>=a[i],那i肯定都堆过了
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 #include<vector> 6 using namespace std; 7 typedef long long ll; 8 typedef pair<int,int> pa; 9 const int maxn=1e5+10; 10 11 inline ll rd(){ 12 ll x=0;char c=getchar();int neg=1; 13 while(c<'0'||c>'9'){ 14 if(c=='-') neg=-1; 15 c=getchar(); 16 } 17 while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar(); 18 return x*neg; 19 } 20 21 int N,a[maxn]; 22 int main(){ 23 // freopen("road.in","r",stdin); 24 // freopen("road.out","w",stdout); 25 int i,j,k; 26 N=rd(); 27 for(i=1;i<=N;i++) a[i]=rd(); 28 int ans=0; 29 for(i=1;i<=N;i++){ 30 if(a[i]>a[i-1]) ans+=a[i]-a[i-1]; 31 } 32 printf("%d ",ans); 33 return 0; 34 }