POJ 2609 Ferry Loading(双塔DP)
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 1807 | Accepted: 509 | Special Judge |
Description
river, and the cars exit from the other end of the ferry.
The cars waiting to board the ferry form a single queue, and the operator directs each car in turn to drive onto the port (left) or starboard (right) lane of the ferry so as to balance the load. Each car in the queue has a different length, which the operator
estimates by inspecting the queue. Based on this inspection, the operator decides which side of the ferry each car should board, and boards as many cars as possible from the queue, subject to the length limit of the ferry. Your job is to write a program that
will tell the operator which car to load on which side so as to maximize the number of cars loaded.
Input
line of input contains the integer 0. The cars must be loaded in order, subject to the constraint that the total length of cars on either side does not exceed the length of the ferry. Subject to this constraint as many cars should be loaded as possible, starting
with the first car in the queue and loading cars in order until no more can be loaded.
Output
and "starboard" if the car is to be directed to the starboard side. If several arrangements of the cars meet the criteria above, any one will do.
Sample Input
50
2500
3000
1000
1000
1500
700
800
0
Sample Output
6
port
starboard
starboard
starboard
port port 双塔DP n个车要么放左边,要么放右边,最大长度不能超过船的长度 这道题目也要记录路径,而且两边的差值有10000,所以必须要用 滚动数组了,要不然内存超限。 dp[i][j] 第i辆车,两边的差距为j<pre name="code" class="html">#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h> using namespace std;
int dp[2][20005];
int d[505];
int e[505];
int a[505];
int ans[505][20005]; int m;
int n;
void dfs(int i,int k)
{
if(i<=0)
return; if(ans[i][k]<0)
{
dfs(i-1,abs(ans[i][k])-2);
cout<<"port"<<endl;
}
else
{
dfs(i-1,ans[i][k]);
cout<<"starboard"<<endl;
}
}
int main()
{
scanf("%d",&m); m*=100;
int x;
scanf("%d",&x);
int n=0;
while(x!=0)
{
a[++n]=x;
scanf("%d",&x);
}
memset(dp,-1,sizeof(dp));
memset(ans,-1,sizeof(ans));
dp[0][0+10000]=0;
int now=1;
for(int i=1;i<=n;i++)
d[i]=10000000;
for(int i=1;i<=n;i++)
{
for(int j=-10000;j<=10000;j++)
{
if(dp[now^1][j+10000]>m) continue;
if(dp[now^1][j+10000]==-1)
continue; if(j<0)
{
if(j+10000-a[i]>=0)//不写会re
{
dp[now][j+10000-a[i]]=dp[now^1][j+10000]+a[i];
if(d[i]>dp[now][j+10000-a[i]])
{
d[i]=dp[now][j+10000-a[i]];//记录第i辆车形成的最小长度
e[i]=j+10000-a[i];//第i辆车形成最小长度的差值,
} ans[i][j+10000-a[i]]=(j+10000)*(-1)-2;//记录路径
}
dp[now][j+10000+a[i]]=dp[now^1][j+10000]+max(0,j+a[i]);
if(d[i]>dp[now][j+10000+a[i]])
{ d[i]=dp[now][j+10000+a[i]];
e[i]=j+10000+a[i];
} ans[i][j+10000+a[i]]=j+10000;
}
else
{
if(j+10000+a[i]<=20000)
{
dp[now][j+10000+a[i]]=dp[now^1][j+10000]+a[i];
if(d[i]>dp[now][j+10000+a[i]])
{ d[i]=dp[now][j+10000+a[i]] ;
e[i]=j+10000+a[i];
}
ans[i][ j+10000+a[i]]=j+10000;
} dp[now][j+10000-a[i]]=dp[now^1][j+10000]+max(0,a[i]-j);
if(d[i]>dp[now][j+10000-a[i]])
{
d[i]=dp[now][j+10000-a[i]];
e[i]= j+10000-a[i];
}
ans[i][j+10000-a[i]]=(j+10000)*(-1)-2;
}
}
memset(dp[now^1],-1,sizeof(dp[now^1]));
now^=1;
if(d[i]>m)
break;
}
int i;
for(i=n;i>=1;i--)
{
if(d[i]<=m)
break;
}
printf("%d\n",i);
dfs(i,e[i]); return 0;
}
POJ 2609 Ferry Loading(双塔DP)的更多相关文章
- POJ 2609 Ferry Loading
双塔DP+输出路径. 由于内存限制,DP只能开滚动数组来记录. 我的写法比较渣,但是POJ能AC,但是ZOJ依旧MLE,更加奇怪的是Uva上无论怎么改都是WA,其他人POJ过的交到Uva也是WA. # ...
- poj-2336 Ferry Loading II(dp)
题目链接: Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3946 Accepted: ...
- 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: 3763 Accepted: 1919 Desc ...
- TOJ 2419: Ferry Loading II
2419: Ferry Loading II Time Limit(Common/Java):1000MS/10000MS Memory Limit:65536KByteTotal Subm ...
- Ferry Loading III[HDU1146]
Ferry Loading IIITime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- ZOJ2401 Zipper 双塔式 DP(双塔DP)
第二次遇到双塔DP,再写一下. (flag是为了避免memset多次导致的时间浪费) #include<cstdio> #include<cstdlib> #include&l ...
- poj 3311(状态压缩DP)
poj 3311(状态压缩DP) 题意:一个人送披萨从原点出发,每次不超过10个地方,每个地方可以重复走,给出这些地方之间的时间,求送完披萨回到原点的最小时间. 解析:类似TSP问题,但是每个点可以 ...
随机推荐
- 重写spring cloud config 本地bootstrap
在spring-cloud中使用了config-server之后,需要在client端加入bootstrap作为配置文件,其中通常包含如下: spring.application.name=ms-as ...
- Linux下双网卡-双外网网关-电信联通双线主机设置
1.实现:通过运营商提供的智能DNS,把电信用户访问时,数据进电信的网卡,出来时也从电信的网关出来,访问联通时,从联通网卡时,联通网卡出.这样速度就会快,实现双线主机的功能. 2.网卡信息:电信IP( ...
- Unity3D_NGUI_性能优化实践_CPU卡顿
http://gad.qq.com/college/articledetail/7083468 博尔特以9.58秒创造了百米世界纪录,假设他是跑酷游戏的角色,卡顿一帧就足以把冠军拱手让人. Unity ...
- 矩阵乘法C语言实现
/* 矩阵乘法C语言实现 Slyar 2009.3.20 */ #include <stdio.h> #include <stdlib.h> /* 给 int 类型定义 ...
- SVN使用(自己总结)
1.第一次导入,注意要新建个readme.txt文件用于记录版本更改,每次导入时 要填写import message 2.第二次增加文件导入时 勾选未受控版本文件(新增加文件) 完成上传就可更新增加的 ...
- JVM日志和参数的理解
写这篇wiki的目的:最近在调整Hbase的JVM,翻了些文档和wiki,想写点东西,给自己和想了解jvm日志和参数的同 学提供些帮助. 一:理解GC日志格式,读GC日志的方法 1:开启日志 -ver ...
- maven仓库国内镜像
<mirror> <id>alimaven</id> <name>aliyun maven</name> ...
- Tomcat下配置多个Project的虚拟主机
server.xml中,添加<Context path="/project2" docBase="D:\MyProjects\project2\WebContent ...
- curl编译
首先下载并解压: wget http://curl.haxx.se/download/curl-7.37.1.tar.gz tar -xzvf curl-7.37.1.tar.gz 编译安装: cd ...
- Oracle(2)数据库
1.使用"||"连接多个字段,合并成一列 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFudGluZ21laQ==/font/5a ...