UVa 1474 - Evacuation Plan 一个题,但是在杭电上能交过,在UVa上交不过……不知道哪里有问题……

将施工队位置和避难所位置排序。

dp[i][j] 代表前 i 个避难所收留前 j 个施工队。

dp[i][j] = min( dp[i - 1][j - 1], dp[i][j - 1] ) + abs( b[i] - a[j] );

内存卡的比较死,要用滚动数组,并且记录路径的path[i][j]只能用bool型。MLE了四五次OTL……

 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> #define LL long long int using namespace std; const int MAXN = ;
const LL INF = (LL) << ; struct Team
{
int id;
LL pos;
}; LL dp[][MAXN];
Team peo[MAXN];
Team shlt[MAXN];
int N, M;
bool path[MAXN][MAXN];
int ans[MAXN], getJ; bool cmp( Team a, Team b )
{
if ( a.pos != b.pos ) return a.pos < b.pos;
return a.id < b.id;
} LL DP()
{
int pre = , cur = ; for ( int i = ; i <= N; ++i )
{
if ( N - i >= M - )
dp[][i] = dp[][i - ] + abs( peo[i].pos - shlt[].pos );
else dp[][i] = INF;
}
dp[][] = ; for ( int i = ; i <= M; ++i )
{
pre ^= ;
cur ^= ;
for ( int j = i; j <= N; ++j )
{
dp[cur][j] = dp[pre][j - ] + abs( peo[j].pos - shlt[i].pos );
path[i][j] = true; if ( j > i && dp[cur][j - ] + abs( peo[j].pos - shlt[i].pos ) < dp[cur][j] )
{
dp[cur][j] = dp[cur][j - ] + abs( peo[j].pos - shlt[i].pos );
path[i][j] = false; } }
}
return dp[cur][N];
} void PrintPath( int i, int j )
{
if ( i == )
{
getJ = j;
return;
} switch ( path[i][j] )
{
case false:
PrintPath( i, j - );
break;
case true:
PrintPath( i - , j - );
break;
} ans[ peo[j].id ] = shlt[i].id; return;
} int main()
{
while ( ~scanf( "%d", &N ) )
{
for ( int i = ; i <= N; ++i )
{
scanf( "%lld", &peo[i].pos );
peo[i].id = i;
}
sort( peo + , peo + N + , cmp ); scanf( "%d", &M );
for ( int j = ; j <= M; ++j )
{
scanf( "%lld", &shlt[j].pos );
shlt[j].id = j;
}
sort( shlt + , shlt + M + , cmp ); printf( "%lld\n", DP() );
PrintPath( M, N );
while ( getJ > )
{
ans[ peo[ getJ ].id ] = shlt[].id;
--getJ;
} for ( int i = ; i <= N; ++i )
{
if ( i != ) putchar(' ');
printf( "%d", ans[i] );
}
puts("");
}
return ;
}

HDU 3757 Evacuation Plan DP的更多相关文章

  1. POJ2175 Evacuation Plan

    Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4617   Accepted: 1218   ...

  2. Codeforces Gym 100002 E "Evacuation Plan" 费用流

    "Evacuation Plan" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  3. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  4. POJ 2175 Evacuation Plan (费用流,负环,消圈法,SPFA)

    http://poj.org/problem?id=2175 Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  5. hdu 2296 aC自动机+dp(得到价值最大的字符串)

    Ring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. HDU 4778 状压DP

    一看就是状压,由于是类似博弈的游戏.游戏里的两人都是绝对聪明,那么先手的选择是能够确定最终局面的. 实际上是枚举最终局面情况,0代表是被Bob拿走的,1为Alice拿走的,当时Alice拿走且满足变换 ...

  7. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

  8. HDOJ(HDU).2546 饭卡(DP 01背包)

    HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...

  9. HDOJ(HDU).2602 Bone Collector (DP 01背包)

    HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...

随机推荐

  1. Keil uVision4 代码编辑器中文字符乱码问题

    MDK-ARM 使用中一直有个很纠结的问题,中文字符支持不好. 比如写代码注释,使用中文删除字符就会只删除一半问题.复制粘贴代码中间有中文就会出现乱码问题. 想过换IAR,新学个IDE也麻烦,上面的问 ...

  2. 关于一道简单的Java 基础面试题的剖析: short s1=1;s1 = s1 +1会报错吗?

    package common; public class ShortTypeTest { /* * @param args */ public static void main(String[] ar ...

  3. Arrays.asList方法总结

    import java.util.Arrays; import java.util.List; /** * * 本类演示了Arrays类中的asList方法 * 通过四个段落来演示,体现出了该方法的相 ...

  4. c++异常详解

    一.什么是异常处理 一句话:异常处理就是处理程序中的错误. 二.为什么需要异常处理,以及异常处理的基本思想 C++之父Bjarne Stroustrup在<The C++ Programming ...

  5. Ubuntu下Apache+php+mysql网站架设详解

    目录 1 基础 2 安装 2.1 安装LAMP 2.2 图形化管理软件(可选) 2.2.1 安装webmin 2.2.2 安装phpmyadmin 3 配置文件路径 3.1 常用命令 3.2 配置ap ...

  6. 从零开始学ios开发(十五):Navigation Controllers and Table Views(中)

    这篇内容我们继续上一篇的例子接着做下去,为其再添加3个table view的例子,有了之前的基础,学习下面的例子会变得很简单,很多东西都是举一反三,稍稍有些不同的内容,好了,闲话少说,开始这次的学习. ...

  7. [Android] ImageView.ScaleType设置图解 【转载】

    ImageView的Scaletype决定了图片在View上显示时的样子,如进行何种比例的缩放,及显示图片的整体还是部分,等等. 设置的方式包括: 1. 在layout xml中定义android:s ...

  8. Ext学习-前后交互模式介绍

    在前后台交互模式的介绍中,实际上就是Store中Proxy相关的内容,比如Ajax提交. 所以详细的文档请参考: Ext学习-基础概念,核心思想介绍   中关于数据模型和MVC结构部分. 作者:sdj ...

  9. oracle中行转列函数

    一.问题描述 有时在“相关子查询中”需要查询某个实体类对应的某个字段有多个值,如果不做行专列查询,会提示返回多个列的错误.例如: 如上图所示,一个组合包,可能对应多个产品,需要你将所对应的多个产品都放 ...

  10. 设置配置文件信息时的classpath

    首先 classpath是指 WEB-INF文件夹下的classes目录  其中:lib和classes下文件访问优先级的问题: lib>classes  classpath 和 classpa ...