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 ...
随机推荐
- html 内联函数宽度设置
width and/or height in tables are not standard anymore; as Ianzz says, they are depreciated. Instead ...
- Android的WebView控件载入网页显示速度慢的究极解决方案
Android的WebView控件载入网页显示速度慢的究极解决方案 [转载来源自http://hi.baidu.com/goldchocobo/] 秒(甚至更多)时间才会显示出来.研究了很久,搜遍了国 ...
- struts开发<struts中的參数传递.三>
不说废话,直接上干货 1.通过set和get传递參数 添加username 和password两个属性并添加set和get方法 package fzl.user.struts.demo; import ...
- window.location.href
WEB设置首页 <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-f ...
- MySQL---insert into select from
INSERT INTO perf_week(node_id,perf_time,pm25,pm10,temp,humi) SELECT node_id,'2016-12-22 11:55:00' AS ...
- Linux wget 命令下载文件
wget是Linux系统中用来下载文件的工具,其功能还是比较多的,能够下载单个文件,也可以分段下载,下面小编将针对wget命令的用法给大家做个实例介绍. 实例1 :下载单个文件 # wget http ...
- 【代码审计】CLTPHP_v5.5.3后台目录遍历漏洞分析
0x00 环境准备 CLTPHP官网:http://www.cltphp.com 网站源码版本:CLTPHP内容管理系统5.5.3版本 程序源码下载:https://gitee.com/chich ...
- Linux eject 命令
eject命令允许在软件控制下弹出可移动媒体(通常是CD-ROM .软盘 .USB等) [root@localhost ~]# eject cdrom //弹出名字为cdrom的设备或者挂载点 [ro ...
- webform的学习(2)
突然回想一下,两周之后放假回家,三周之后重返学习,四周之后就要真正的面对社会,就这样有好多的舍不得在脑海中回旋,但是又是兴奋的想快点拥有自己的小生活,似乎太多的人在说程序的道路甚是艰难,我不知道我的选 ...
- sqlite的一个Unable to Open database file的坑爹错误
今天,被sqlite的一个机制给坑了.本人用C语言写的cgi程序去访问sqlite数据库,读取没有问题,但是插入新纪录和更新数据就不行,在服务器上直接对数据库进行增删查改则没有任何问题.但浏览器上访问 ...