hdu 4362 Dragon Ball

Dragon Ball

                                                          Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/32768 K
(Java/Others)

Total Submission(s): 2204    Accepted Submission(s): 770

Problem Description

Sean has got a Treasure map which shows when and where the dragon balls will appear. some dragon balls will appear in a line at the same time for each period.Since the time you got one of them,the other dragon ball will disappear so he can only and must get
one Dragon ball in each period.Digging out one ball he will lose some energy.Sean will lose |x-y| energy when he move from x to y.Suppose Sean has enough time to get any drogan ball he want in each period.We want to know the minimum energy sean will lose to
get all period’s dragon ball.

Input

In the first line a number T indicate the number of test cases.Then for each case the first line contain 3 numbers m,n,x(1<=m<=50,1<=n<=1000),indicate m period Dragon ball will appear,n dragon balls for every period, x is the initial location of sean.Then two
m*n matrix. For the first matrix,the number in I row and J column indicate the location of J-th Dragon ball in I th period.For the second matrix the number in I row and J column indicate the energy sean will lose for J-th Dragon ball in I-th period.

Output

For each case print a number means the minimum energy sean will lose.

Sample Input

1
3 2 5
2 3
4 1
1 3
1 1
1 3
4 2

Sample Output

8

Author

FZU

Source

2012 Multi-University Training Contest 7

   题意:

         m个周期,每个周期出现n个龙珠,刚开始在x位置,从x位置到y位置消耗的能量值是两者差的绝对值+在y位    置的龙珠要吸收的能量值,每个周期必须要在一个龙珠的位置。

 

   题解:

           很容易想到动态规划方程a[i][k].dp=min(a[i][k].dp,(a[i-1][j].dp+|a[i][k].x-a[i-1][j].x|+a[i][k].v)),这样直接用复    杂度为O(m*n*n)会超时,需要优化,把绝对值去掉,会出现两种情况

   1.a[i][k].x>=a[i-1][j].x(前一个周期龙珠的位置小于后一个周期龙珠的位置)

         这时a[i-1][j].dp-a[i-1][j].x+a[i][k].x+a[i][k].v,设mini=a[i-1][j].dp-a[i-1][j].x  此时a[i][k].dp=mini+a[i][k].x+a[i]      [k].v 对a[i][k].dp,a[i][k].x和a[i][k].v为固定的,在所有的a[i][k].x>=a[i-1][j].x中,只需找到最小的mini就可以
         了,此时对所有j,只需遍历一遍j就可以了,不需要每个k都遍历一遍j了,因为对后一个k值,其i-1行x值小于现在      的x值的,只会多在前面的基础上多数,前面的最小的只需与后来多的比较就可以了。

   2.a[i][k].x<=a[i-1][j].x(前一周期龙珠的位置大于后一个周期龙珠的位置)

          这时a[i-1][j].dp+a[i-1][j].x-a[i][k].x+a[i][k].v,设mini=a[i-1][j].dp-a[i-1][j].x,此时只需在所有的前一周期龙珠的    位置大于后一个周期龙珠的位置中找最小的mini就可以了。

  代码:

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int inf=1999999999;
struct node
{
    int x;
    int v;
    int dp;
}a[55][1100];
int bbs(int x)
{
    if(x<0)
    x=-x;
    return x;
}
bool cmp(node l,node r)
{
    return l.x<r.x;
}
int main()
{
    int t,n,m,x;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&m,&n,&x);
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&a[i][j].x);
                a[i][j].dp=inf;
            }
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&a[i][j].v);
            }
        }
        sort(a[1]+1,a[1]+1+n,cmp);
        for(int i=1;i<=n;i++)
        a[1][i].dp=bbs(x-a[1][i].x)+a[1][i].v;//第1周期特殊处理
        for(int i=2;i<=m;i++)
        {
            sort(a[i]+1,a[i]+1+n,cmp);
            //前一个周期龙珠的位置小于后一个周期龙珠的位置
            int j=1;
            int mini=inf;
            for(int k=1;k<=n;k++)
            {
                     while(j<=n&&a[i][k].x>=a[i-1][j].x)
                     {
                         mini=min(mini,(a[i-1][j].dp-a[i-1][j].x));
                         j++;
                     }
                     a[i][k].dp=min(a[i][k].dp,(mini+a[i][k].x+a[i][k].v));
            }
            //前一周期龙珠的位置大于后一个周期龙珠的位置
            j=n;
            mini=inf;
            for(int k=n;k>=1;k--)
            {
                while(j>=1&&a[i-1][j].x>=a[i][k].x)
                {
                    mini=min(mini,(a[i-1][j].dp+a[i-1][j].x));
                    j--;
                }
                a[i][k].dp=min(a[i][k].dp,(mini-a[i][k].x+a[i][k].v));
            }
        }
        int ans=inf;
        for(int i=1;i<=n;i++)
        {
            ans=min(ans,a[m][i].dp);
        }
        printf("%d\n",ans);
    }
    return 0;
}

时间: 2024-11-08 22:33:15

hdu 4362 Dragon Ball的相关文章

HDU 4362 Dragon Ball 贪心DP

Dragon Ball Problem Description Sean has got a Treasure map which shows when and where the dragon balls will appear. some dragon balls will appear in a line at the same time for each period.Since the time you got one of them,the other dragon ball wil

HDU 4362 Dragon Ball(维护最小值DP优化)

 题意: 在连续的 n 秒中,每秒会出现 m 个龙珠,出现之后会立即消失,知道了第一秒所在的位置,每从一个位置移动到另一个位置的时候,消耗的价值为 abs(i-j), 知道了次出现的龙珠的价值,问 n 秒之后得到的最大价值是多少. 思路:这道题朴素的做法时间复杂度为O(n*n*m)勉强可以水过去,正解应该是用单调队列的思路维护最小值优化. 由状态转移方程dp[i][j] = min{ dp[i-1][k] + abs(pos[i-1][k]-pos[i][j]) } + cost[i][j]

HDU 4362 Dragon Ball 线段树

#include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <map> #include <set> #include <stack> #include <vector> #include <iostream> #include <algorithm> using namespace st

HDU 3635 Dragon Balls(并查集)

Dragon Balls Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 64   Accepted Submission(s) : 26 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Five hundred years later

hdu 3635 Dragon Balls(并查集应用)

Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to gather all of the dragon balls together. His country has N cities and there are exactly N dragon bal

HDU 3635 Dragon Balls (并查集)

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3360    Accepted Submission(s): 1303 Problem Description Five hundred years later, the number of dragon balls will increase unexpect

HDU 3635 Dragon Balls 七龙珠 Union Find算法

孙悟空要寻找七龙珠,这回是七龙珠的增强版了,因为这些龙珠会衍生,最后不止七颗龙珠了. 悟空带着布玛的龙珠雷达探测器出发了,却发现布玛的龙珠雷达探测器的程序太垃圾了,所以找到我们这些ACM高手为龙珠雷达探测器写个程序,要求可以显示某颗龙珠所在的城市的位置,该龙珠所在的城市共有多少颗龙珠,龙珠移动过的次数. 布玛是个有钱人啊,写个程序我要价5百万,不算过分吧.因为本程序需要用到Union Find(并查集)算法,而且最困难的部分是如何压缩路径,不压缩路径自然容易做到,要压缩路径可以使得程序加快很多,

hdu 3635 Dragon Balls 【基础带权并查集】

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3461    Accepted Submission(s): 1348 Problem Description Five hundred years later, the number of dragon balls will increase unexpect

hdu 1556Color the ball (树状数组,更新区间,查询单点)

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12566    Accepted Submission(s): 6294 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气