双塔DP+输出路径。

由于内存限制,DP只能开滚动数组来记录。

我的写法比较渣,但是POJ能AC,但是ZOJ依旧MLE,更加奇怪的是Uva上无论怎么改都是WA,其他人POJ过的交到Uva也是WA。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<stack>
#include<algorithm>
using namespace std; int dp[][+];
struct P
{
int x,y;
int f;
}pre[][+];
int sum;
int a[],tot; void read()
{
while()
{
scanf("%d",&a[tot+]);
if(a[tot+]==) break;
tot++;
} for(int i=;i<=tot;i++)
for(int j=;j<=*sum;j++)
pre[i][j].f=-; for(int j=;j<=*sum;j++) dp[][j]=dp[][j]=0x7FFFFFFF;
} void init()
{
sum=sum*;
tot=;
} void work()
{
int now=;
dp[][a[]+sum]=;
pre[][a[]+sum].f=;
dp[][-a[]+sum]=;
pre[][-a[]+sum].f=; for(int i=;i<=tot;i++)
{
now=now^;
for(int j=;j<=*sum;j++) dp[now][j]=0x7FFFFFFF;
for(int j=;j<=*sum;j++)
{
if(dp[now^][j]==0x7FFFFFFF) continue; int tmp=j-sum; if(tmp>=)
{
if(dp[now^][j]+tmp+a[i]<=sum&&dp[now^][j]<dp[now][tmp+a[i]+sum])
{
dp[now][tmp+a[i]+sum]=dp[now^][j];
pre[i][tmp+a[i]+sum].x=i-;
pre[i][tmp+a[i]+sum].y=j;
pre[i][tmp+a[i]+sum].f=;
} if(max(tmp,a[i])+dp[now^][j]<=sum&&dp[now^][j]+min(tmp,a[i])<dp[now][tmp-a[i]+sum])
{
dp[now][tmp-a[i]+sum]=dp[now^][j]+min(tmp,a[i]);
pre[i][tmp-a[i]+sum].x=i-;
pre[i][tmp-a[i]+sum].y=j;
pre[i][tmp-a[i]+sum].f=;
}
} else
{
tmp=-tmp;
if(dp[now^][j]+tmp+a[i]<=sum&&dp[now^][j]<dp[now][-(tmp+a[i])+sum])
{
dp[now][-(tmp+a[i])+sum]=dp[now^][j];
pre[i][-(tmp+a[i])+sum].x=i-;
pre[i][-(tmp+a[i])+sum].y=j;
pre[i][-(tmp+a[i])+sum].f=;
} if(max(tmp,a[i])+dp[now^][j]<=sum&&dp[now^][j]+min(tmp,a[i])<dp[now][a[i]-tmp+sum])
{
dp[now][a[i]-tmp+sum]=dp[now^][j]+min(tmp,a[i]);
pre[i][a[i]-tmp+sum].x=i-;
pre[i][a[i]-tmp+sum].y=j;
pre[i][a[i]-tmp+sum].f=;
} }
}
}
} void output()
{
bool flag=;
int now_i,now_j;
for(int i=tot;i>=;i--)
{
for(int j=*sum;j>=;j--)
{
if(pre[i][j].f!=-)
{
now_i=i;
now_j=j;
flag=;
break;
}
}
if(flag) break;
} if(flag==) {printf("0\n");return;}
printf("%d\n",now_i); stack<int>S; while()
{
if(pre[now_i][now_j].f==-) break;
S.push(pre[now_i][now_j].f); int newx,newy;
newx=pre[now_i][now_j].x;
newy=pre[now_i][now_j].y;
now_i=newx;
now_j=newy;
} int k=;
int left=;
int right=; while(!S.empty())
{
if(S.top()==)
{
printf("port\n");
left=left+a[k];k++;
}
else
{
printf("starboard\n");
right=right+a[k];k++;
}
S.pop();
}
} int main()
{
scanf("%d",&sum);
init();
read();
work();
output();
return ;
}

POJ 2609 Ferry Loading的更多相关文章

  1. POJ 2609 Ferry Loading(双塔DP)

    Ferry Loading Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1807   Accepted: 509   Sp ...

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

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

  3. Ferry Loading III[HDU1146]

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

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

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

  5. poj-2336 Ferry Loading II(dp)

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

  6. TOJ 2419: Ferry Loading II

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

  7. Ferry Loading II_贪心

    Description Before bridges were common, ferries were used to transport cars across rivers. River fer ...

  8. [POJ2336]Ferry Loading II

    题目描述 Description Before bridges were common, ferries were used to transport cars across rivers. Rive ...

  9. POJ 3377 Ferry Lanes

    虽然它出现在dp专场里···但是我第一反应是一道最短路题···不过幸好它出现在dp专场里···因为我不怎么会dijstra什么的··· 题意:一条河上有N+1对码头,每个相邻码头之间需要一定时间到达, ...

随机推荐

  1. DEAMONTOOLS: installation

    installing daemontools .. adding -I /usr/include/errno.h to last first line of conf-cc mkdir -p /pac ...

  2. map.entry<k,v>小用法(转)

    你是否已经对每次从Map中取得关键字然后再取得相应的值感觉厌倦?使用Map.Entry类,你可以得到在同一时间得到所有的信息.标准的Map访问方法如下: Set keys = map.keySet( ...

  3. LightOJ 1234 Harmonic Number 调和级数部分和

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1234 Sample Input Sample Output Case : Case : ...

  4. android--listview设置高度

    android:layout_height="25dp"这么设置根本就没有用. 我们必须要设置android:minHeight="25dp"这样才行.

  5. 详细的css命名规则,专业点吧

    头:header内容:content/container尾:footer导航:nav侧栏:sidebar栏目:column页面外围控制整体布局宽度:wrapper左右中:left right cent ...

  6. redmine配置邮件

    作为一个项目管理平台,必须能够通知项目成员有关项目和任务的各种状态变化.这也是一种典型的观察者模式.开发人员可以跟踪某个任务,当任务状态.备注.文档.优先级等变化时开发人员就可以得到通知. 对于新配置 ...

  7. Connecting Universities

    Connecting Universities Treeland is a country in which there are n towns connected by n - 1 two-way ...

  8. u-boot添加一个hello命令

    1.在common目录下建立一个cmd_hello.c文件 2.仿照/common/cmd_bootm.c文件修改,把cmd_bootm.c头文件复制过来 3.再复制do_bootm.U_BOOT_C ...

  9. 转:XPath路径表达式

    XPath 使用路径表达式来选取 XML 文档中的节点或节点集.节点是通过沿着路径 (path) 或者步 (steps) 来选取的. XML 实例文档 我们将在下面的例子中使用这个 XML 文档. & ...

  10. 前端复制功能的若干 -- document.execCommand()

    最近涨停科技公司实习,由于backend基础太弱...强行前端了一把..搞了两周才搞下页面里copy的功能,期间有些琐碎,恐忘,记录在此. 目前copy主流有四种方式:ZeroClipboard,Cl ...