Ferry Loading
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1807   Accepted: 509   Special Judge

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. Two lanes of cars drive onto the ferry from one end, the ferry crosses the
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

The first line of input contains a single integer between 1 and 100: the length of the ferry (in metres). For each car in the queue there is an additional line of input specifying the length of the car (in cm, an integer between 100 and 3000 inclusive). A final
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

The first line of outuput should give the number of cars that can be loaded onto the ferry. For each car that can be loaded onto the ferry, in the order the cars appear in the input, output a line containing "port" if the car is to be directed to the port side
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)的更多相关文章

  1. POJ 2609 Ferry Loading

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

  2. poj-2336 Ferry Loading II(dp)

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

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

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

  4. POJ-2336 Ferry Loading II(简单DP)

    Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3763 Accepted: 1919 Desc ...

  5. TOJ 2419: Ferry Loading II

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

  6. Ferry Loading III[HDU1146]

    Ferry Loading IIITime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...

  7. 【POJ 3071】 Football(DP)

    [POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted ...

  8. ZOJ2401 Zipper 双塔式 DP(双塔DP)

    第二次遇到双塔DP,再写一下. (flag是为了避免memset多次导致的时间浪费) #include<cstdio> #include<cstdlib> #include&l ...

  9. poj 3311(状态压缩DP)

    poj  3311(状态压缩DP) 题意:一个人送披萨从原点出发,每次不超过10个地方,每个地方可以重复走,给出这些地方之间的时间,求送完披萨回到原点的最小时间. 解析:类似TSP问题,但是每个点可以 ...

随机推荐

  1. intellij中常用的快捷键

    intellij快捷键

  2. JSP中out.write()和out.print()的区别

    out对象的类型是JspWriter.JspWriter继承了java.io.Writer类. 1)print方法是子类JspWriter,write是Writer类中定义的方法: 2)重载的prin ...

  3. glibc/libc/blib区别

    转自:http://blog.csdn.net/yasi_xi/article/details/9899599 [glibc 和 libc] glibc 和 libc 都是 Linux 下的 C 函数 ...

  4. ORACLE的显式游标与隐式游标

    1)查询返回单行记录时→隐式游标: 2)查询返回多行记录并逐行进行处理时→显式游标 显式游标例子: DECLARE CURSOR CUR_EMP IS SELECT * FROM EMP; ROW_E ...

  5. MapReduce编程实例4

    MapReduce编程实例: MapReduce编程实例(一),详细介绍在集成环境中运行第一个MapReduce程序 WordCount及代码分析 MapReduce编程实例(二),计算学生平均成绩 ...

  6. linux学习笔记29---命令watch

    watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行.在Linux下,watch是周期性的执行下个 ...

  7. redis存储对象与对象序列化详解

    redis主要存储类型最常用的五种数据类型: String Hash List Set Sorted set redis存储对象序列化和反序列化 首先来了解一下为什么要实现序列化 为什么要实现序列化接 ...

  8. 【转】【Mysql学习】之Mac上用终端使用mySQL

    收藏了几篇文章,以供查阅: 在此感谢: m0_38017925:<Mac上用终端使用mySQL> xiamu03:<在MacOS上使用终端操作数据库>

  9. JVM参数MetaspaceSize的误解

    前言 昨天谢照东大神在群里提出一个问题:怎么查看Metaspace里具体包含的是什么,起因是他的某个服务设置了-XX:MetaspaceSize=512m -XX:MaxMetaspaceSize=5 ...

  10. Istio官方文档中文版

    Istio官方文档中文版 http://istio.doczh.cn/ https://istio.io/docs/concepts/what-is-istio/goals.html 为什么要使用Is ...