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问题,但是每个点可以 ...
随机推荐
- Current thread must be set to single thread apartment (STA) mode before OLE,当前线程不在单线程单元中,因此无法实例化 ActiveX 控件“8856f961-340a-11d0-a96b-00c04fd705a2”。
Add the STAThreadAttribute attribute on the Main method. This attribute is required if your program ...
- R内存扩展 win7内存扩展
安装包 imdiskinst 文件 램디스크 사용http://www.ltr-data.se/ http://cruciancar.blog.me/150101634586 --TEMP 변수 TE ...
- migrate的使用
安装完yii2后,需要创建一张user表,这里用migrate创建 1.在dos窗口下,先cd到项目的目录,比如E:\>cd E:\Visual-AMP-x64\www\blog\blog 2. ...
- Jquery学习笔记(6)--jquery中attr和prop的区别【精辟】
jquery中attr和prop的区别 在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答案很 ...
- powerDesigner建表时选择不同数据库类型
l 使用powerDesigner创建表模型的时候可选择数据库类型 如图设置为mysql:
- 基于quick-cocos2d-x的LuaSocket范例
这是一个 luasocket 范例. 为了便于使用,我封装了 luasocket 到 cc.net.SocketTCP 类中.这个范例展示如何使用 cc.net.SocketTCP . 同时,在本范例 ...
- Xcode模拟iPhone教程!
iOS 开发者常常会使用模拟器来进行调试,当然这就少不了Mac电脑中的Xcode软件了,今天PC6小编就给大家带来在Mac系统下如何快速启动iOS模拟器的使用教程: 一.如何启动iOS模拟器 1.在L ...
- 流媒体服务器开发笔记(2)--RTCP协议介绍
http://blog.sina.com.cn/s/blog_53061af00100o2no.html ——————————————————————————————————————————————— ...
- 【vijos】1729 Knights(匈牙利)
https://vijos.org/p/1729 这题好奇葩,为嘛N开到30就会re啊..........n<=26吗.... sad 因为根据棋子的分布,能攻击的一定各在一黑白格上,所以直接二 ...
- 【BZOJ】3299: [USACO2011 Open]Corn Maze玉米迷宫(bfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=3299 映射一下传送门即可.. #include <cstdio> #include &l ...