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. 增强的PuTTY 以及 自定义主题

    PuTTY很早之前就没有更新了(0.62),因为都是开源的所以有人branch出来做了增强,如这个PuTTY tray,增加了超链等功能: https://puttytray.goeswhere.co ...

  2. ubuntu 下dbus的环境搭建和使用

    从https://launchpad.net/ubuntu/+source/dbus/1.10.6-1ubuntu2下载需要的dbus包,然后解压,./configure make && ...

  3. Java反射在JVM的实现

    1. 什么是Java反射,有什么用?反射使程序代码能够接入装载到JVM中的类的内部信息,允许在编写与执行时,而不是源代码中选定的类协作的代码,是以开发效率换运行效率的一种手段.这使反射成为构建灵活应用 ...

  4. 【BZOJ 1005】[HNOI2008]明明的烦恼

    Description 自从明明学了树的结构,就对奇怪的树产生了兴趣...... 给出标号为1到N的点,以及某些点最终的度数,允许在任意两点间连线,可产生多少棵度数满足要求的树? Input 第一行为 ...

  5. Export Farm Solution wsp Files SharePoint 2007 and 2010

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")$farm = [Microsof ...

  6. 常用的机器学习&数据挖掘知识点【转】

    转自: [基础]常用的机器学习&数据挖掘知识点 Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Le ...

  7. 在SQL SErver中实现数组功能

    T-SQL象数组一样处理字符串.分割字符串    在日常的编程过程中,数组是要经常使用到的.在利用SQL对数据库进行操作时,有时就想在SQL使用数组,比如将1,2,3,4,5拆分成数组.可惜的是在T- ...

  8. android中实现Parcelable序列化步骤

    import java.io.Serializable; import java.text.DecimalFormat; import android.os.Parcel; import androi ...

  9. android 设置半透明

    对于Button和ImageButton 还有一些View 设置半透明或者透明都是通过 android:background="#b0000000" 这是就是半透明 android ...

  10. WebApi参数传递总结

    在WebAPI中,请求主体(HttpContent)只能被读取一次,不被缓存,只能向前读取的流. 举例子说明: 1. 请求地址:/?id=123&name=bob 服务端方法: void Ac ...