POJ-2336 Ferry Loading II(简单DP)
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)的更多相关文章
- poj 2336 Ferry Loading II ( 【贪心】 )
Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3704 Accepted: 1884 ...
- poj-2336 Ferry Loading II(dp)
题目链接: Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3946 Accepted: ...
- TOJ 2419: Ferry Loading II
2419: Ferry Loading II Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal Subm ...
- POJ 2609 Ferry Loading(双塔DP)
Ferry Loading Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1807 Accepted: 509 Sp ...
- POJ 3267 The Cow Lexicon 简单DP
题目链接: http://poj.org/problem?id=3267 从后往前遍历,dp[i]表示第i个字符到最后一个字符删除的字符个数. 状态转移方程为: dp[i] = dp[i+1] + 1 ...
- poj 1050 To the Max (简单dp)
题目链接:http://poj.org/problem?id=1050 #include<cstdio> #include<cstring> #include<iostr ...
- POJ 2609 Ferry Loading
双塔DP+输出路径. 由于内存限制,DP只能开滚动数组来记录. 我的写法比较渣,但是POJ能AC,但是ZOJ依旧MLE,更加奇怪的是Uva上无论怎么改都是WA,其他人POJ过的交到Uva也是WA. # ...
- [POJ2336]Ferry Loading II
题目描述 Description Before bridges were common, ferries were used to transport cars across rivers. Rive ...
- poj 1417(并查集+简单dp)
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2087 Accepted: 640 Descrip ...
随机推荐
- 使用srvany.exe把程序安装成windows服务的方法
http://mazhihui.iteye.com/blog/1294431 srvany.exe是什么? srvany.exe是Microsoft Windows Resource Kits工具集的 ...
- Java实现在复制文件时使用进度条
在对大文件操作时,可能会需要些时间,此时为用户提供进度条提示是非常常见的一项功能,这样用户就可以了解操作文件需要的时间信息.本实例为大家介绍了在复制大的文件时使用的进度条提示,需要注意的是,只有在读取 ...
- flexbox父盒子justify-content属性
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- C语言编程规范—命名规则
C是一门朴素的语言,你使用的命名也应该这样.与Modula-2和Pascal程序员不同,C程序员不使用诸如“ThisVariableIsATemporaryCounter”这样“聪明”的名字.C程序员 ...
- php-fpm 记录慢执行日志
有时候我们访问一个 php 的网站很慢,要想知道慢的原因,需要配置 php-fpm.conf,记录执行慢的日志: [root@localhost ~]$ cat /usr/local/php/etc/ ...
- 使用 Splash
Splash 简介与安装 Splash Lua 脚本 Splash 对象属性 Splash 对象方法 Splash API 调用 Splash 负载均衡
- Pycharm按装
1.python 官方 2.下载完成后点击exe 安装 3.按装完成后在cmd中输入 python 1.如果显示python版本 那么就安装成功 2.如果出现"python"不是外 ...
- [转]redis的三种启动方式
来源:https://www.cnblogs.com/pqchao/p/6549510.html redis的启动方式1.直接启动 进入redis根目录,执行命令: #加上‘&’号使red ...
- C++ template —— 智能指针(十二)
在管理动态分配的内存时,一个最棘手的问题就是决定何时释放这些内存,而智能指针就是用来简化内存管理的编程方式.智能指针一般有独占和共享两种所有权模型.-------------------------- ...
- 基于thinkphp和ajax的省市区三级联动
练习,就当练习. 省市区三级联动,样式如下图所示: 1,导入两个js文件并且导入数据库文件. 两个js文件分别是jquery-2.1.4.min.js和jquery-1.js,数据库文件,见附件. 2 ...