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. Configure Log Shipping

    准备工作 两台装有的Windows Server 2012R2以及SQL Server 2012的服务器 下载评估版 Windows Server 2012 R2 下载 Microsoft SQL S ...

  2. mysql简单操作一

    MySQL的一些简单管理: 启动MySQL服务: sudo start mysql 停止MySQL服务: sudo stop mysql 修改 MySQL 的管理员密码: sudo mysqladmi ...

  3. 【netstream】探索数据传输对象1

    什么是“从当前流中读取一个字符串.字符串有长度前缀,一次 7 位地被编码为整数.” 来探索一下: 写一段简单的程序: FileStream fs= new FileStream("d:\\q ...

  4. mysql数据库本地化操作

    <?php if(!defined('SITE_PATH')){ define('SITE_PATH',dirname(dirname(__FILE__))); } $dbconfig=incl ...

  5. Backbone.Events—纯净MVC框架的双向绑定基石

    Backbone.Events-纯净MVC框架的双向绑定基石 为什么Backbone是纯净MVC? 在这个大前端时代,各路MV*框架如雨后春笋搬涌现出来,在infoQ上有一篇 12种JavaScrip ...

  6. android开发修改相机扫描二维码框的高宽

    我用的是网上一个现成的例子,可以直接用,但是高宽不合适,现在主流都是大屏幕手机了,所以需要更改. 找到CameraManager 类,更改下面的方法 public Rect getFramingRec ...

  7. android 开发,多个线程共用一个handler

    在做项目过程中,突然发现,项目中启动了多个线程,但是只有一个handler,而不需要每一个线程单独开一个handler,记下笔记: handler = new Handler() { @Overrid ...

  8. Oppotunity land---China

    China is a land of opportunity.Following the development of China,every sector has made their contri ...

  9. win8 telnet VirtualBox中的redhat9

    1. VirtualBox设置网络连接为“桥接网卡”,并且此网卡要为win8正在使用的网卡(比如我的电脑上使用的就是无线网卡,则选择网卡时也要用无线网卡) 2. 在redhat的终端里,运行ifcon ...

  10. Eclipse maven工程 Missing artifact com.sun:tools:jar:1.5.0:system 解决方法

    今天同事在使用eclipse,引入一个新的maven工程时报错:      Missing artifact com.sun:tools:jar:1.6.0:system   这个问题很奇怪,相同的代 ...