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问题,但是每个点可以 ...
随机推荐
- sql getdate()生成单据号
select replace( replace( replace( replace(convert(varchar(23),getdate(),121),'-',''),':',''),' ','') ...
- CI框架源代码阅读笔记7 配置管理组件 Config.php
原文见这里:http://www.cnblogs.com/ohmygirl/p/CIRead-7.html 一个灵活可控的应用程序中,必定会存在大量的可控參数(我们称为配置),比如在CI的主配置文件里 ...
- MVC数据验证Model Validation
Required必须项验证属性 [Required] public string FirstName { get; set; } //ID编号 [ScaffoldColumn(false)] [Req ...
- FreeSWITCH小结:关于sip的UDP、TCP与MTU
1.关于SIP的UDP与MTU的关系 如果sip消息的大小超过了MTU,则有可能被网络中的某一节点分片,而UDP处理分片会有很大的问题,从而导致sip消息传输失败.要解决该问题的话,两种方案: 1)减 ...
- unity, 不要用TextMesh,用图片代替
<方块鸭快跑>(见:http://www.cnblogs.com/wantnon/p/4596222.html)1.0版本开始界面中鸭子的speech bubble中的文字用的是TextM ...
- BootStrap modal() 如何根据返回的HTML宽度自动调整宽度?
首先声明,如果真的这么做了也就失去了 bootstrap 多分辨率适配的好处.bootstrap 的 modal 窗口能够自动在不同分辨率下用不同的宽度,这就是它的特色呢. 以默认大小的 modal ...
- SET QUOTED_IDENTIFIER OFF语句的作用 转载
SET QUOTED_IDENTIFIER ON SELECT * FROM "USER" WHERE a='netasp' SET QUOTED_IDENTIFIER ON SE ...
- 选择如何的系统更能适合App软件开发人员?
手机这个词早已经同吃喝玩乐一样.成为了人们生活中的必备元素. 尤其是iPhone一炮走红之后,不但手机世界发生了巨大变化,整个科技产业似乎都格局性的改变.直至今日,手机市场的竞争更是日趋白炽化,这就给 ...
- CSU 1335: 高桥和低桥 (二分查找,树状数组)
Description 有个脑筋急转弯是这样的:有距离很近的一高一低两座桥,两次洪水之后高桥被淹了两次,低桥却只被淹了一次,为什么?答案是:因为低桥太低了,第一次洪水退去之后水位依然在低桥之上,所以不 ...
- 解决eclipse偶尔无视breakpoint的行为
一般是如果你使用了T[]这样的参数列表,也就是generic array作为参数,你就算给函数打了断点,有时也会被eclipse无视 比如如下代码,你在调试main的时候,eclipse就会把doPa ...