UVALive 4987---Evacuation Plan(区间DP)

题目链接

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2988

problem Description

Flatland government is building a new highway that will be used to transport weapons from its main weapon plant to the frontline in order to support the undergoing military operation against its neighbor country Edgeland. Highway is a straight line and there are n construction teams working at some points on it. During last days the threat of a nuclear attack from Edgeland has significantly increased. Therefore the construction office has decided to develop an evacuation plan for the construction teams in case of a nuclear attack. There are m shelters located near the constructed highway. This evacuation plan must assign each team to a shelter that it should use in case of an attack. Each shelter entrance must be securely locked from the inside to prevent any damage to the shelter itself. So, for each shelter there must be some team that goes to this shelter in case of an attack. The office must also supply fuel to each team, so that it can drive to its assigned shelter in case of an attack. The amount of fuel that is needed is proportional to the distance from the team’s location to the assigned shelter. To minimize evacuation costs, the office would like to create a plan that minimizes the total fuel needed. Your task is to help them develop such a plan.

Input

The input file contains several test cases, each of them as described below. The first line of the input file contains n — the number of construction teams (1 ≤ n ≤ 4000). The second line contains n integer numbers - the locations of the teams. Each team’s location is a positive integer not exceeding 109 , all team locations are different. The third line of the input file contains m — the number of shelters (1 ≤ m ≤ n). The fourth line contains m integer numbers — the locations of the shelters. Each shelter’s location is a positive integer not exceeding 109 , all shelter locations are different. The amount of fuel that needs to be supplied to a team at location x that goes to a shelter at location y is equal to |x − y|.

Output

For each test case, the output must follow the description below. The first line of the output file must contain z — the total amount of fuel needed. The second line must contain n integer numbers: for each team output the number of the shelter that it should be assigned to. Shelters are numbered from 1 to m in the order they are listed in the input file.

Sample Input

3

1 2 3

2

2 10

Sample Output

8

1 1 2

题意:输入n  然后输入n个施工队的位置(一维坐标) 然后输入m 再输入m个防御点的位置(一维坐标),1<=m<=n<=4000  一维坐标小于1e9  现在让所有的施工队进入防御点,且每个防御点必须有施工队进入,求所有施工队走的最小距离和,并输出每个施工队去的防御点编号;

思路:区间DP,定义dp[i][j]  表示前i个施工队进入j个防御点的最小距离和,那么有状态转移方程:dp[i][j]=max{dp[i-1][j-1],dp[i-1][j]}+abs(a[i]-b[j]) 注意要先对输入的施工队和防御点进行从小到大的排序;

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int n,m;
long long dp[4006][4006];
bool vis[4006][4006];

struct Node
{
    long long x;
    int id;
    int t;
    bool operator < (const Node & tt) const
    { return x < tt.x; }
}a[4005],b[4005];

bool cmp(const Node s1,const Node s2)
{
    return s1.id<s2.id;
}

void print(int x,int y)
{
    if(y==1&&x==1){
        a[x].t=b[y].id;
        return ;
    }
    print(x-1,y-1+vis[x][y]);
    a[x].t=b[y].id;
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&a[i].x);
            a[i].id=i;
        }
        sort(a+1,a+n+1);
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            scanf("%lld",&b[i].x);
            b[i].id=i;
        }
        sort(b+1,b+m+1);

        memset(dp,0,sizeof(dp));
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m&&j<=i;j++)
            {
                if(j==1)
                {
                    dp[i][j]=dp[i-1][j]+abs(a[i].x-b[j].x);
                    vis[i][j]=true;
                }
                else if(j==i)
                {
                    dp[i][j]=dp[i-1][j-1]+abs(a[i].x-b[j].x);
                    vis[i][j]=false;
                }
                else
                {
                    dp[i][j]=min(dp[i-1][j],dp[i-1][j-1])+abs(a[i].x-b[j].x);
                    vis[i][j]=(dp[i-1][j]>dp[i-1][j-1])?false:true;
                }
            }
        }
        cout<<dp[n][m]<<endl;
        print(n,m);
        sort(a+1,a+n+1,cmp);
        for(int i=1;i<=n;i++)
            printf("%d%c",a[i].t,(i==n)?‘\n‘:‘ ‘);
    }
    return 0;
}
时间: 2024-10-25 20:22:28

UVALive 4987---Evacuation Plan(区间DP)的相关文章

UVALive - 3363 String Compression 区间DP

题目大意:有一串字符串,现在有一种转换规则,如果字符串中出现循环的子串,可以将其转化为 :子串数量(子串) 现在问这个字符串的最短长度 解题思路:区间dp,然后分类讨论,这题的难点是如何再进行缩减 将情况分为两种 一种是区间刚好符合缩减情况的,找出该区间的循环节,看能否继续缩减即可 另一种情况就是普通的区间DP了 #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #de

UVALive 3516 Exploring Pyramids 区间dp+计数原理

题目链接:点击打开链接 给定多叉树的先序遍历结果,求多叉树的同构数 思路:区间dp import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; public class Main { int min(int a,int b){return a>b?b:a;} int max(int a,int b){return a>b?a:b;} long min(long a,long b){retur

UVALive 3516 Exploring Pyramids (区间dp)

#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; #define ll long long const int maxn = 310; const ll mod = 1e9; char s[maxn]; ll dp[maxn][maxn]; ll solve(int i, int j) { if(i == j) retu

hdu 4412 Sky Soldiers(区间DP)

Sky Soldiers Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 559    Accepted Submission(s): 181 Problem Description An airplane carried k soldiers with parachute is plan to let these soldiers j

uva 10003 Cutting Sticks 简单区间dp

// uva 10003 Cutting Sticks 区间dp // 经典的区间dp // dp(i,j)表示切割小木棍i-j所需要的最小花费 // 则状态转移为dp(i,j) = min{dp(i,k) + dp(k,j) + a[j]-a[i]) // 其中k>i && k<j // a[j] - a[i] 为第一刀切割的代价 // a[0] = 0,a[n+1] = L; // dp数组初始化的时候dp[i][i+1]的值为 0,这表示 // 每一段都已经是切割了的,不

黑书例题 Fight Club 区间DP

题目可以在bnuoj.soj等OJ上找到. 题意: 不超过40个人站成一圈,只能和两边的人对战.给出任意两人对战的输赢,对于每一个人,输出是否可能是最后的胜者. 分析: 首先序列扩展成2倍,破环成链. dp[i][j]表示i和j能够相遇对打,那么dp[i][i+n]为真代表可以成为最后胜者. 枚举中间的k,若i和j都能和k相遇,且i和j至少一人能打赢k,那么i和j可以相遇. 复杂度o(n^3) 1 #include<cstdio> 2 #include<cstring> 3 usi

算法复习——区间dp

感觉对区间dp也不好说些什么直接照搬讲义了2333 例题: 1.引水入城(洛谷1514) 这道题先开始看不出来到底和区间dp有什么卵关系···· 首先肯定是bfs暴力判一判可以覆盖到哪些城市····无解直接输出···有解得话就要想想了···· 这道题关键是要发现··如果一个蓄水池所在城市可以覆盖到一些沙漠城市···那么这些沙漠城市肯定是一段····不然假设有一个城市是断开的而两边都被同一个蓄水池流出的水覆盖,这个城市四周的城市都肯定比它矮···(不理解举个反例吧···反正我举不出来)···然后就

合并石子 区间dp水题

合并石子 链接: nyoj 737 描述: 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程只能每次将相邻的两堆石子堆成一堆,每次合并花费的代价为这两堆石子的和,经过N-1次合并后成为一堆.求出总的代价最小值. tags:最基本的区间dp,这题范围小,如果n大一些,还是要加个平行四边行优化. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring&g

Luogu P2734 游戏 A Game 区间DP

P2734 游戏 A Game 题目背景 有如下一个双人游戏:N(2 <= N <= 100)个正整数的序列放在一个游戏平台上,游戏由玩家1开始,两人轮流从序列的任意一端取一个数,取数后该数字被去掉并累加到本玩家的得分中,当数取尽时,游戏结束.以最终得分多者为胜. 题目描述 编一个执行最优策略的程序,最优策略就是使玩家在与最好的对手对弈时,能得到的在当前情况下最大的可能的总分的策略.你的程序要始终为第二位玩家执行最优策略. 输入输出格式 输入格式: 第一行: 正整数N, 表示序列中正整数的个数