HDOJ 4362 —— DP + 二分 | 单调栈

Dragon Ball

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


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
 

Recommend
zhuyuanchen520
 
题意:一共有m个时刻,每一时刻在数轴上都会出现n个龙珠,然后取一个,其它的就会消失,下一时刻你要从当前位置x,去y位置去另一个龙珠,所消耗的能量有两部分,一部分是从x到y耗费的能量是|x-y|,另一部分是取龙珠的时候。求取完m个龙珠耗的能量最小值。 
第一种思路是DP+二分:
源自某位大神:
分析:常规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)
我的理解就是去预处理出xL和rL,然后二分。
dp[i][j] = min { dp[i-1][k] + pos[i][j] - pos[i-1][k] } + cost[i][j]
         = min { dp[i-1][k] - pos[i-1][k] } + pos[i][j] + cost[i][j]

dp[i][j] = min { dp[i-1][k] + pos[i-1][k] - pos[i][j] } + cost[i][j]
         = min { dp[i-1][k] + pos[i-1][k] } - pos[i][j] + cost[i][j]
dp[i-1][k]-pos[i-1][k]是个确定的值,相当于求位置在pos[i][j]左边的上一层状态中值最小的。同理,右面的可解。
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
//#define mid ((l + r) >> 1)
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
#define bug(s) cout<<"#s = "<< s << endl;
const int MAXN = 1010;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
#define mod 1000000007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
///#pragma comment(linker, "/STACK:102400000,102400000")
struct Point
{
    int x , p;
}a[55][MAXN];
int d[MAXN][MAXN] ,m , n , xx , xL[MAXN] ,xR[MAXN];
bool cmp(Point A , Point B)
{
    if(A.x < B.x)return true;
    return false;
}
int BSearch(int i , int key)
{
    int L , R;
    L = 0 , R = n - 1;
    while(L <= R)
    {
        int mid = (L + R) >> 1;
        if(a[i][mid].x < key)L = mid + 1;
        else if(a[i][mid].x > key)R = mid - 1;
        else return mid;
    }
    return L;
}
void DP()
{
    sort(a[0] , a[0] + n , cmp);
    FOR(i , 0 , n)d[0][i] = abs(a[0][i].x- xx) + a[0][i].p;
    FOR(i  ,1,  m)
    {
        sort(a[i] , a[i] + n , cmp);
        xL[0] = d[i - 1][0] - a[i - 1][0].x;
        FOR(j , 1, n)xL[j] = min(d[i - 1][j] - a[i - 1][j].x , xL[j - 1]);
        xR[n - 1] = d[i - 1][n - 1] + a[i - 1][n - 1].x;
        REPP(j , n - 2 , 0)xR[j] = min(d[i - 1][j] + a[i - 1][j].x , xR[j + 1]);
        FOR(j , 0 , n)
        {
            int k = BSearch(i - 1 , a[i][j].x);
            if(k == n)d[i][j] = xL[n - 1] + a[i][j].x + a[i][j].p;
            else if(k == 0)d[i][j] = xR[0] - a[i][j].x + a[i][j].p;
            else
            {
                if(a[i - 1][k].x == a[i][j].x)
                {
                    d[i][j] = min(xL[k] + a[i][j].x + a[i][j].p ,xR[k]- a[i][j].x + a[i][j].p);
                }
                else
                {
                    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);
                }
            }
        }
    }
}
int main()
{
    //ios::sync_with_stdio(false);
    #ifdef Online_Judge
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif // Online_Judge
    int t;
    cin >> t;
    while(t--)
    {
        scanf("%d%d%d" , &m , &n,  &xx);
        FOR(i , 0 , m)FOR(j ,0 , n)scanf("%d" , &a[i][j].x);
        FOR(i , 0 , m)FOR(j , 0 , n)scanf("%d" , &a[i][j].p);
        DP();
        int ans = INF;
        FOR(i ,0 , n)if(ans > d[m - 1][i])ans = d[m - 1][i];
        printf("%d\n" , ans);
    }
    return 0;
}
第二种思路是DP+单调栈:
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值