poi2014 Freight

poi2014 Freight

Freight

The train stations in Upper Bytown and Lower Bytown are connected with a single track rail link. It takes the train   minutes to travel between them in either direction. However, the trains departing a station have to be spaced at least one minute apart. Moreover, at all times, all the trains on the rail link have to go in the same direction.

According to the timetable at our disposal,   cargo trains destined for Lower Bytown are to pass through the Upper Bytown. They are to be loaded with goods in Lower Bytown and then return to Upper Bytown. For simplicity, we assume that loading the goods on the train takes virtually no time.

We are to determine the minimum possible time of the last train returning to Upper Bytown.

 

 

输入

 

The first line of the standard input contains two integers N,S(1<=N<=1 000 000,1<=S<=10^9) , separated by a single space, that specify the number of trains and the one-way travel time respectively.

 

The second line contains   integers T1,T2…Tn(0<=T1<=T2..<=Tn<=10^9) , separated by a single space, that specify the arrival times of successive trains at the Upper Bytown station.

 

 

输出

 

Your program should print out a single line with a single integer to the standard output: the minimum possible time of the last train returning to Upper Bytown.

 

 

样例输入

3 4
1 8 11

样例输出

20

提示

 

 

 

 

来源

poi2014


solution

可以看出车是一批批回来的,不会在那等。

那么考虑dp

f[i]=min(max(a[i],f[j]+i-j-1)+2*s+i-j-1)

把a[i]取成max(a[i],a[i-1]+1) 不影响答案

且保证了a[i]>f[j]+i-j-1 时 转移递增

又因为a[i]<f[j]+i-j-1时f[i] 时 转移递增  (f[j]>2*j)
所以转移成一个V型

 

找到那个点即可

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 1000006
using namespace std;
int n,a[maxn];
long long f[maxn],S;
int main(){
    cin>>n>>S;
    a[0]=-1;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        a[i]=max(a[i],a[i-1]+1);
        f[i]=1e15;
    }
    int j=1;
    for(int i=1;i<=n;i++){
        while(j<i&&f[j]+i-j-1<a[i])j++;
        f[i]=min(f[i],a[i]+2*S+i-j);// 其实是向j-1转移
        if(f[j]+i-j-1>=a[i])f[i]=min(f[i],f[j]+i-j-1+2*S+i-j-1);
    }
    cout<<f[n]<<endl;
    return 0;
}

 

posted @ 2018-10-01 08:47 liankewei 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值