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问题,但是每个点可以 ...
随机推荐
- 解决nginx到后端服务器Connection: close问题
以下是官方原文: By default, NGINX redefines two header fields in proxied requests, “Host” and “Connection”, ...
- Python selenium -- cookie处理
转自:http://www.cnblogs.com/fnng/p/3269450.html 本节重点: driver.get_cookies() 获得cookie信息 add_cookie(cooki ...
- mongodb - 查看正在执行的操作
查看正在执行的操作 db.currentOp() 查看系统执行的操作 db.currentOp(True) kill正在执行的操作 db.killOp(<operation id>) 示例 ...
- 怎样防止应用因获取IDFA被AppStore拒绝
由于Appstore禁止不使用广告而採集IDFA的app上架,友盟提供IDFA版和不含IDFA版两个SDK,两个SDK在数据上并没有差异.採集IDFA是为了防止今后由于苹果可能禁止眼下使用的openu ...
- Powershell - 获取OS版本信息和catpion信息
Environment 获取 OSversion: $OSVersion = [System.Environment]::OSVersion.Version WMI获取Caption: $OSCap ...
- linux 批量替换内容
sed -i "s/被替换的内容/替换的内容/g" `grep "被替换的内容" -rl 目录` -r = 搜索下级目录
- Atitit.使用引擎加脚本架构的设计 使用php,js来开发桌面程序。。
Atitit.使用引擎加脚本架构的设计 使用php,js来开发桌面程序.. 1. 引擎加脚本架构 跨平台,桌面与web的优势1 2. 架构桌面引擎(java,c#)2 3. php桌面引擎要点2 3. ...
- [k8s]kubelet最佳实战
kubelet端口解析: 10250 –port: kubelet服务监听的端口,api会检测他是否存活 10248 –healthz-port: 健康检查服务的端口 10255 –read-only ...
- maven打包classes为jar
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-p ...
- spring aop切面编程实现操作日志步骤
1.在spring-mvc.xml配置文件中打开切面开关: <aop:aspectj-autoproxy proxy-target-class="true"/> 注意: ...