UVALive 4987 --- Evacuation Plan (interval DP), evacuationplan

Source: Internet
Author: User
Tags integer numbers

UVALive 4987 --- Evacuation Plan (interval DP), evacuationplan

Question Link

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 shoshould 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 wocould 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 shoshould be assigned. 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

Enter n and then enter the positions of n construction teams (one-dimensional coordinates), then input m and then enter the positions of m Defense points (one-dimensional coordinates ), 1 <= m <= n <= 4000 the one-dimensional coordinate is less than 1e9. Now, all construction teams are allowed to enter the Defense points, and each defense point must have a construction team, calculate the minimum distance and number of each construction team's defense point;

Train of Thought: range DP, define dp [I] [j] to indicate the minimum distance and the first I construction team enters j Defense points, then there is a state transition equation: dp [I] [j] = max {dp [I-1] [J-1], dp [I-1] [j]} + abs (a [I]-B [j]) note that the input construction teams and Defense points should be sorted in ascending order;

The Code is as follows:

#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;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.