hdu 4362 Dragon Ball(DP+优化,4级)
2013-07-22 10:45
381 查看
C - Dragon Ball
Time Limit:1500MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 4362
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
思路:dp[i][j]=min{dp[i-1][k]+abs(arr[i][j].x-arr[i-1][k].x)+arr[i][j].w}
二分法
优先队列
Time Limit:1500MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Submit Status Practice HDU 4362
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
思路:dp[i][j]=min{dp[i-1][k]+abs(arr[i][j].x-arr[i-1][k].x)+arr[i][j].w}
二分法
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> using namespace std; const int mm=1100; const int oo=1e9; int dp[55][mm]; int cas; int ll[mm],rr[mm]; int fabs(int x) { if(x<0)return -x; return x; } class node { public:int wei,w; bool operator <(const node&x)const { if(wei==x.wei)return w<x.w; return wei<x.wei; } }f[55][mm]; int Q[mm],pos; int look(int x,int l,int r,int num) { int mid; while(l<=r) { mid=(l+r)/2; if(f[x][mid].wei<num)l=mid+1; else r=mid-1; } return r; } int main() { int n,m,x; while(~scanf("%d",&cas)) { while(cas--) { scanf("%d%d%d",&m,&n,&x); for(int i=0;i<m;++i) for(int j=0;j<n;++j) scanf("%d",&f[i][j].wei); for(int i=0;i<m;++i) for(int j=0;j<n;j++) scanf("%d",&f[i][j].w); for(int i=0;i<m;++i) sort(f[i],f[i]+n); for(int i=0;i<n;++i) dp[0][i]=f[0][i].w+fabs(f[0][i].wei-x); for(int i=1;i<m;++i) { int _min=oo; for(int j=0;j<n;++j) { _min=min(_min,dp[i-1][j]-f[i-1][j].wei); ll[j]=_min; } _min=oo; for(int j=n-1;j>=0;--j) { _min=min(_min,dp[i-1][j]+f[i-1][j].wei); rr[j]=_min; } for(int j=0;j<n;++j) { dp[i][j]=oo; if(f[i][j].wei<=f[i-1][0].wei) dp[i][j]=min(dp[i][j],rr[0]-f[i][j].wei+f[i][j].w);///左边大 else if(f[i][j].wei>=f[i-1][n-1].wei) dp[i][j]=min(dp[i][j],ll[n-1]+f[i][j].wei+f[i][j].w); else { int xx=look(i-1,0,n-1,f[i][j].wei);///比其小的第一个 dp[i][j]=min(dp[i][j],min(ll[xx]+f[i][j].wei+f[i][j].w,rr[xx+1]-f[i][j].wei+f[i][j].w)); } } } int ans=oo; for(int i=0;i<n;++i) ans=min(dp[m-1][i],ans); printf("%d\n",ans); } } return 0; }
优先队列
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> using namespace std; const int mm=1100; const int oo=1e9; int dp[55][mm]; int cas; int fabs(int x) { if(x<0)return -x; return x; } class node { public:int wei,w; bool operator <(const node&x)const { if(wei==x.wei)return w<x.w; return wei<x.wei; } }f[55][mm]; int Q[mm],pos; int main() { int n,m,x; while(~scanf("%d",&cas)) { while(cas--) { scanf("%d%d%d",&m,&n,&x); for(int i=0;i<m;++i) for(int j=0;j<n;++j) scanf("%d",&f[i][j].wei); for(int i=0;i<m;++i) for(int j=0;j<n;j++) scanf("%d",&f[i][j].w); for(int i=0;i<=m;++i) for(int j=0;j<=n;++j) dp[i][j]=oo; for(int i=0;i<m;++i) sort(f[i],f[i]+n); for(int i=0;i<n;++i) dp[0][i]=f[0][i].w+fabs(f[0][i].wei-x); for(int i=1;i<m;++i) { pos=0; Q[pos++]=oo;int k=0; for(int j=0;j<n;++j) { while(k<n) { if(f[i][j].wei<f[i-1][k].wei)break; ///当前位置比上一层大 while(pos) { if(Q[pos-1]>dp[i-1][k]-f[i-1][k].wei) --pos; else break; } Q[pos++]=dp[i-1][k]-f[i-1][k].wei; ++k; } dp[i][j]=Q[0]+f[i][j].w+f[i][j].wei; } pos=0; Q[pos++]=oo;k=n-1; /*dp[i][j]=min(dp[i-1][j]+f[i-1][k].wei-f[i][j].wei+f[i][j].w)*/ for(int j=n-1;j>=0;--j) { while(k>=0) { if(f[i][j].wei>f[i-1][k].wei)break;///当前位置要比上一层小 while(pos) { if(Q[pos-1]>dp[i-1][k]+f[i-1][k].wei)///找到比当前小的为值,因为比它大的肯不是最优 --pos; else break; } Q[pos++]=dp[i-1][k]+f[i-1][k].wei; --k; } dp[i][j]=min(dp[i][j],Q[0]-f[i][j].wei+f[i][j].w); } } //puts("++++"); // for(int i=0;i<m;++i) // for(int j=0;j<n;++j) //printf("%d%c",dp[i][j],j==n-1?'\n':' '); int ans=oo; for(int i=0;i<n;++i) ans=min(dp[m-1][i],ans); printf("%d\n",ans); } } return 0; }
相关文章推荐
- HDU - 4362 Dragon Ball(单调队列优化的DP)@
- hdu 4362 Dragon Ball 单调队列优化 dp
- HDU 4362 Dragon Ball【dp优化】
- HDU 4362 Dragon Ball(维护最小值DP优化)
- HDU 4362 Dragon Ball (DP)
- hdu 4362 Dragon Ball(dp)
- HDU 4362 单调队列优化DP
- hdu 4362 Dragon Ball 很裸的DP
- HDU 4362 Dragon Ball 贪心DP
- hdu 4362(单调队列优化dp)
- hdu 5013 优化疑问+dp
- HDU 2829 Lawrence 斜率优化DP
- HDU-3480 Division 【DP+斜率优化(二维)】
- 2017多校第二场 HDU 6049 Sdjpx Is Happy 区间DP,复杂度优化
- hdu 4374 One hundred layer(单调队列优化dp)
- hdu 2993 简单斜率优化dp 学了一手变态输入法
- HDU 2829 Lawrence(斜率优化DP)
- HDU 3507 斜率优化dp
- HDU 3341 Lost's revenge (AC自动机 + dp[优化好多= =])
- hdu 3962(AC自动机+矩阵优化dp)