noip2018 day1T1 铺设道路
题目:https://www.luogu.org/problemnew/show/P5019
今年noip考了一道原题:
在考场上我想了暴力的方法:
这题暴力可以过
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=110000;
const int M=20;
int n,d[N],ans,mn[N][M];
int find(int l,int r){//找 l,r中最小数
int L=log(r-l+1)/log(2);
return min(mn[l][L],mn[r-(1<<L)+1][L]);
}
void sov(int l,int r,int now){
if(l>r) return ;
if(l==r) {
ans+=d[l]-now;
return ;
}
int minx=find(l,r);int last=l-1;
ans+=(minx-now);
for(int i=l;i<=r;i++){
if(d[i]==minx){
sov(last+1,i-1,minx);
last=i;
}
}
sov(last+1,r,minx);
}
int main(){
// freopen("road.in","r",stdin);
//freopen("road.out","w",stdout);
scanf("%d",&n);ans=0;
for(int i=1;i<=n;i++){
scanf("%d",&d[i]);
}
for(int i=1;i<=n;i++) mn[i][0]=d[i];
for(int j=1;(1<<j)<=n;j++) {
for(int i=1;i+(1<<j)-1<=n;i++) mn[i][j]=min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);//,printf("%d %d %d\n",i,j,mn[i][j]);
}
sov(1,n,0);
printf("%d\n",ans);
return 0;
}