来源
题目描述
给了一艘船,该船一次性可运输n辆车,过去需要t分钟(返回亦然)。一共有m辆车需要运输,接下来m行给出每辆车到达港口的时间,求解最短运输完所有车辆的时间和最少运输次数。
Sample Input
2 2 10 10 0 10 20 30 40 50 60 70 80 90 2 10 3 10 30 40
Sample Output
100 5 50 2
拿到题读完显然的动态规划,不过题目由于要求解两个子问题就比较头疼。
稍加思索后可以发现,本题无论如何决策,
“船渡河所需要的时间t是不变的”
,即本问题
没有后效性
。
所以我们只需要保证该问题子结构最优即可,在子结构最优(即时间最优)的情况下,找出次数即可。
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN=1440+100; //不是很懂1440这个数字是什么意思...
int n,t,m;
int time[MAXN],tot[MAXN],c[MAXN];
void DP(){
for(int i=1;i<=m;i++){
time[i]=max(time[max(i-n,0)],c[i])+2*t;
tot[i]=tot[max(i-n,0)]+1;
}
printf("%d %d\n",time[m]-t,tot[m]); //最后一辆车不用返回
}
int main(){
int T; scanf("%d",&T);
while(T--){
memset(time,0,sizeof(time));
memset(tot,0,sizeof(tot));
scanf("%d%d%d",&n,&t,&m);
for(int i=1;i<=m;i++) scanf("%d",&c[i]);
DP();
}
return 0;
}