hdu4362 Dragon Ball (dp)

Dragon Ball

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 924    Accepted Submission(s): 354


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
 
题意:一共有m个时刻,每一时刻在数轴上都会出现n个龙珠,然后取一个,其它的就会消失,下一时刻你要从当前位置x,去y位置去另一个龙珠,所消耗的能量有两部分,一部分是从x到y耗费的能量是|x-y|,另一部分是取龙珠的时候。求取完m个龙珠耗的能量最小值。
 
分析:常规dp方程很容易想 d[i][j]=min(d[i-1][k] + |a[i-1][k].x - a[i][j].x| + a[i][j].p);
如果用这个方程时间复杂度是O(m*n*n);写的不好很容易TLE,不过我队友貌似小小的优化下就过了  = =!!
小技巧:这里的优化主要是去绝对值,观察结构可以知道 d[i-1][k] 和 a[i-1][k].x 才是关键,他们明显可以预处理,因为都是上一层的;于是为了去绝对值,对上一层数轴上的 x 值排序,上面每个元素xL[j]=min(xL[j-1] , d[i-1][j]-a[i-1][j].x)  ,xR[j]=min(xR[j+1] , d[i-1][j]+a[i-1][j].x) 。
然后对于每个d[i][j] 只要通过a[i][j].x判断在上一层什么位置就行了,对于有序的序列查找位置,二分就ok了 ,于是时间复杂度为O(m*n*logn)
 
View Code
 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 #define MAXM 55
 7 #define MAXN 1010
 8 
 9 using namespace std;
10 
11 struct point
12 {
13     int x,p;
14 }a[MAXM][MAXN];
15 int d[MAXM][MAXN],m,n,xx;
16 int xL[MAXN],xR[MAXN];
17 
18 bool cmp(point A,point B)
19 {
20     if(A.x<B.x) return true;
21     return false;
22 }
23 
24 int find(int i,int key)
25 {
26     int L,R,M;
27     L=0;R=n-1;
28     while(L<=R)
29     {
30         M=(L+R)>>1;
31         if(a[i][M].x<key) L=M+1;
32         else if(a[i][M].x>key) R=M-1;
33         else return M;
34     }
35     return L;
36 }
37 
38 void work()
39 {
40     int i,j,k;
41     sort(a[0],a[0]+n,cmp);
42     for(i=0;i<n;i++)
43         d[0][i]=abs(a[0][i].x-xx)+a[0][i].p;
44     for(i=1;i<m;i++)
45     {
46         sort(a[i],a[i]+n,cmp);
47         xL[0]=d[i-1][0]-a[i-1][0].x;
48         for(j=1;j<n;j++)
49         {
50             xL[j]=min(d[i-1][j]-a[i-1][j].x,xL[j-1]);
51         }
52         xR[n-1]=d[i-1][n-1]+a[i-1][n-1].x;
53         for(j=n-2;j>=0;j--)
54         {
55             xR[j]=min(d[i-1][j]+a[i-1][j].x,xR[j+1]);
56         }
57 
58         for(j=0;j<n;j++)
59         {
60             k=find(i-1,a[i][j].x);
61             if(k==n) d[i][j]=xL[n-1]+a[i][j].x+a[i][j].p;
62             else if(k==0) d[i][j]=xR[0]-a[i][j].x+a[i][j].p;
63             else
64             {
65                 if(a[i-1][k].x==a[i][j].x)
66                     d[i][j]=min(xL[k]+a[i][j].x+a[i][j].p,xR[k]-a[i][j].x+a[i][j].p);
67                 else
68                     d[i][j]=min(xL[k-1]+a[i][j].x+a[i][j].p,xR[k]-a[i][j].x+a[i][j].p);
69             }
70         }
71     }
72 }
73 
74 int main()
75 {
76     int test,i,j;
77     scanf("%d",&test);
78     while(test--)
79     {
80         scanf("%d%d%d",&m,&n,&xx);
81         for(i=0;i<m;i++)
82             for(j=0;j<n;j++)
83                 scanf("%d",&a[i][j].x);
84         for(i=0;i<m;i++)
85             for(j=0;j<n;j++)
86                 scanf("%d",&a[i][j].p);
87         work();
88         int ans=1000000000;
89         for(i=0;i<n;i++)
90             if(ans>d[m-1][i])
91                 ans=d[m-1][i];
92         printf("%d\n",ans);
93     }
94     return 0;
95 }

 

转载于:https://www.cnblogs.com/zhourongqing/archive/2012/08/15/2640982.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值