NOIP2018 道路铺设

NOIP2018 道路铺设_第1张图片

analysys

DP

f [ i ] f[i] f[i]为填满1~i需要的天数


当 d [ i ] < d [ i − 1 ] 时 , f [ i ] = f [ i − 1 ] 当 d [ i ] > d [ i − 1 ] 时 , f [ i ] = f [ i − 1 ] + d [ i ] − d [ i − 1 ] 当d[i]d[i-1]时,f[i]=f[i-1]+d[i]-d[i-1] d[i]<d[i1],f[i]=f[i1]d[i]>d[i1],f[i]=f[i1]+d[i]d[i1]

code

#include
using namespace std;
#define ll long long
#define loop(i,start,end) for(register int i=start;i<=end;++i)
template<typename T>void read(T &x){
    x=0;char r=getchar();T neg=1;
    while(r>'9'||r<'0'){if(r=='-')neg=-1;r=getchar();}
    while(r>='0'&&r<='9'){x=(x<<1)+(x<<3)+r-'0';r=getchar();}
    x*=neg;
}
const int maxn=100000+10;
int d[maxn],n;
ll dp[maxn];
int main(){
    read(n);
    loop(i,1,n)read(d[i]);
    loop(i,1,n){
        if(d[i]<=d[i-1])dp[i]=dp[i-1];
        else if(d[i]>d[i-1])dp[i]=dp[i-1]+d[i]-d[i-1];
    }printf("%lld\n",dp[n]);
    return 0;
}

你可能感兴趣的:(小蒟蒻的noip回头路)