前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU-3757-Evacuation Plan

HDU-3757-Evacuation Plan

作者头像
f_zyj
发布2018-01-09 10:48:10
5100
发布2018-01-09 10:48:10
举报
文章被收录于专栏:ACM小冰成长之路

ACM模版

描述

描述
描述

题解

image.png
image.png

代码

代码语言:javascript
复制
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;

const int MAXN = 4e3 + 10;
const long long INF = 0x3f3f3f3f3f3f3f3f;

short path[MAXN][MAXN];
long long dp[MAXN];

struct node
{
    long long dis;
    int pos, she;
};

int n, m;
node a[MAXN], b[MAXN];

bool cmp_dis(const node &a, const node &b)
{
    return a.dis < b.dis;
}

bool cmp_pos(const node &a, const node &b)
{
    return a.pos < b.pos;
}

void get_she(int i, int j)
{
    if (i != 1)
    {
        get_she(i - 1, path[i][j]);
    }
    a[i].she = b[j].pos;
}

int main(int argc, char const *argv[])
{
    int T;
    scanf("%d", &T);

    while (T--)
    {
        memset(dp, 0x3f, sizeof(dp));

        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
        {
            scanf("%lld", &a[i].dis);
            a[i].pos = i;
        }

        scanf("%d", &m);
        for (int i = 1; i <= m; i++)
        {
            scanf("%lld", &b[i].dis);
            b[i].pos = i;
        }

        sort(a + 1, a + 1 + n, cmp_dis);
        sort(b + 1, b + 1 + m, cmp_dis);

        dp[1] = abs(a[1].dis - b[1].dis);
        for (int i = 2; i <= n; i++)
        {
            int t = min(i, m);
            for (int j = t; j >= 1; j--)
            {
                if (dp[j] < dp[j - 1])
                {
                    dp[j] = dp[j] + abs(a[i].dis - b[j].dis);
                    path[i][j] = j;
                }
                else
                {
                    dp[j] = dp[j - 1] + abs(a[i].dis - b[j].dis);
                    path[i][j] = j - 1;
                }
            }
        }

        printf("%lld\n", dp[m]);

        get_she(n, m);

        sort(a + 1, a + 1 + n, cmp_pos);
        for (int i = 1; i < n; i++)
        {
            printf("%d ", a[i].she);
        }
        printf("%d\n", a[n].she);
    }

    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年10月16日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 描述
  • 题解
  • 代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档