Ferry Loading II

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 3763 Accepted: 1919

Description

Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river’s current. Cars drive onto the ferry from one end, the ferry crosses the river, and the cars exit from the other end of the ferry.

There is a ferry across the river that can take n cars across the river in t minutes and return in t minutes. m cars arrive at the ferry terminal by a given schedule. What is the earliest time that all the cars can be transported across the river? What is the minimum number of trips that the operator must make to deliver all cars by that time?

Input

The first line of input contains c, the number of test cases. Each test case begins with n, t, m. m lines follow, each giving the arrival time for a car (in minutes since the beginning of the day). The operator can run the ferry whenever he or she wishes, but can take only the cars that have arrived up to that time.

Output

For each test case, output a single line with two integers: the time, in minutes since the beginning of the day, when the last car is delivered to the other side of the river, and the minimum number of trips made by the ferry to carry the cars within that time.

You may assume that 0 < n, t, m < 1440. The arrival times for each test case are in non-decreasing order.

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

其实如果想到状态转移方程还是比较简单的,但是dp题目难的就是状态转移方程啊。我想了挺长时间,还是看了别人的博客。但是这道题目让我对dp题目又有了深刻的认识。我之前之所以想不出来是因为,我把状态弄混了,dp[i]表示第i辆车运送到对岸所花的时间,这个是

车的状态,而不是船的状态。所以所有的思考都应当围绕车子。这辆车被送到对岸,只有两种情况,要么是它不用等,船等着它,它一到就被带走。要么它等着穿,船一来它就跟船走。这就是车子的状态。

#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h> using namespace std;
#define MAX 1<<30
int dp[2000];
int bp[2000];
int a[2000];
int n,t,m;
int main()
{
int c;
scanf("%d",&c);
while(c--)
{
scanf("%d%d%d",&n,&t,&m);
for(int i=1;i<=m;i++)
scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
dp[i]=bp[i]=MAX;
dp[0]=-t;
dp[1]=a[1]+t;
bp[1]=1;
for(int i=2;i<=m;i++)
{
for(int j=max(0,i-n);j<i;j++)
{
dp[i]=min(dp[i],max(dp[j]+t,a[i])+t);
bp[i]=min(bp[i],bp[j]+1);
}
}
printf("%d %d\n",dp[m],bp[m]);
}
return 0;
}

POJ-2336 Ferry Loading II(简单DP)的更多相关文章

  1. poj 2336 Ferry Loading II ( 【贪心】 )

    Ferry Loading II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3704   Accepted: 1884 ...

  2. poj-2336 Ferry Loading II(dp)

    题目链接: Ferry Loading II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3946   Accepted: ...

  3. TOJ 2419: Ferry Loading II

    2419: Ferry Loading II  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KByteTotal Subm ...

  4. POJ 2609 Ferry Loading(双塔DP)

    Ferry Loading Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1807   Accepted: 509   Sp ...

  5. POJ 3267 The Cow Lexicon 简单DP

    题目链接: http://poj.org/problem?id=3267 从后往前遍历,dp[i]表示第i个字符到最后一个字符删除的字符个数. 状态转移方程为: dp[i] = dp[i+1] + 1 ...

  6. poj 1050 To the Max (简单dp)

    题目链接:http://poj.org/problem?id=1050 #include<cstdio> #include<cstring> #include<iostr ...

  7. POJ 2609 Ferry Loading

    双塔DP+输出路径. 由于内存限制,DP只能开滚动数组来记录. 我的写法比较渣,但是POJ能AC,但是ZOJ依旧MLE,更加奇怪的是Uva上无论怎么改都是WA,其他人POJ过的交到Uva也是WA. # ...

  8. [POJ2336]Ferry Loading II

    题目描述 Description Before bridges were common, ferries were used to transport cars across rivers. Rive ...

  9. poj 1417(并查集+简单dp)

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2087   Accepted: 640 Descrip ...

随机推荐

  1. [Scikit-learn] 2.5 Dimensionality reduction - ICA

    理论学习: 独立成分分析ICA历史 Ref: Lecture 15 | Machine Learning (Stanford) - NG From: https://wenku.baidu.com/v ...

  2. Faulty Odometer(九进制数)

    Faulty Odometer Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9301   Accepted: 5759 D ...

  3. 分表需要解决的问题 & 基于MyBatis 的轻量分表落地方案

    分表:垂直拆分.水平拆分 垂直拆分:根据业务将一个表拆分为多个表. 如:将经常和不常访问的字段拆分至不同的表中.由于与业务关系密切,目前的分库分表产品均使用水平拆分方式. 水平拆分:根据分片算法将一个 ...

  4. PMP模拟考试-1

    1. A manufacturing project has a schedule performance index (SPI) of 0.89 and a cost performance ind ...

  5. FFMPEG AVRational

    FFMPEG的很多结构中有AVRational time_base;这样的一个成员,它是AVRational结构的 typedef struct AVRational{ int num; ///< ...

  6. 【RF库测试】Encode String To Bytes&Decode Bytes To String& should be string&should be unicode string &should not be string

    场景1:判断类型 r ${d} set variable \xba\xcb\xbc\xf5\xcd\xa8\xb9\xfd #核减通过 Run Keyword And Continue On Fail ...

  7. Four Ways to Create a Thread

    Blaise Pascal Magazine Rerun #5: Four Ways to Create a Thread   This article was originally written ...

  8. iOS开发-VFL初窥

    VFL是苹果为了简化Autolayout的编码而推出的抽象语言,在上一篇博客中我们发现如果使用NSLayoutConstraint来添加约束是非常繁琐的. 一个简单的Frame需要添加四个NSLayo ...

  9. osgEarth设置模型旋转角度

    #include<windows.h> #include <osgViewer/Viewer> #include <osgEarthDrivers/gdal/GDALOp ...

  10. 在 Ubuntu 中安装 MySQL 指南

    安装MySQL 在Ubuntu上可以使用Ubuntu Software Center或者apt命令来安装MySQL,两种方式都十分方便. 1. 使用Ubuntu Software Center:打开U ...