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. Oracle查询出最最近一次的一条记录

    需求:从一个表中查询数据,得到的数据为最新的一条记录. -------------建立测试表 --drop table TB ),dtDate date) -------------插入测试数据 ,' ...

  2. Java线程通信——wait() 和 notify()

    Object类中有关线程通信的方法有两个notify方法和三个wait方法,官方解释: void notify() Wakes up a single thread that is waiting o ...

  3. 微软职位内部推荐-Android Developer

    微软近期Open的职位: Position: SDE II or Senior SDE -- Mobile Products Android/WP Contact Person: Winnie Wei ...

  4. cocos2dx中加载图片资源的方法,和从内存中获取已经加载的图片资源的方法

    游戏中通常需要将常用的资源如:声音,图片,plist文件,提前加载进内存,以加快游戏的流畅度 1.预加载声音: SimpleAudioEngine::getInstance()->preload ...

  5. iOS 10 版本适配问题收集-b

    随着iOS10发布的临近,大家的App都需要适配iOS10,下面是我总结的一些关于iOS10适配方面的问题,如果有错误,欢迎指出. 1.系统判断方法失效: 在你的项目中,当需要判断系统版本的话,不要使 ...

  6. android 设置叠加父级响应点击事件

    最近做android项目时,需要让button的后面的relatelayout可以点击,但是虽然把button设置成了focusable="false",relatelayout中 ...

  7. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  8. linux配置mysql,tomcat命令vi

    [root@yangchehome bin]# ./mysqld_safe -user=mysql & [1] 17135 [root@yangchehome bin]# 140316 16: ...

  9. 企业运营对 DevOps 的「傲慢与偏见」

    摘要:出于各种原因,并非所有人都信任 DevOps .有些人觉得 DevOps 只不过给开发者改善产品提供了一个途径而已,还有的人觉得 DevOps 是一堆悦耳的空头支票,甚至有人认为 DevOps ...

  10. qsort()与besearch()

    功 能: 使用快速排序例程进行排序 头文件:stdlib.h 用 法: void qsort(void *base,int nelem,int width,int (*fcmp)(const void ...