Shoot Game(区间dp)

题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6365]


Description

You are playing a shooting game. The rules of the game are like this: You are in a two-dimensional plane and stand at (0,0). There are some obstacles above the x-axis. The location of each obstacle can be expressed as a tuple (H,L,R), It means there is an obstacle at the height of H, interval L to R. The ith obstacle with Wi defense power.

You can shoot out “Energy beam of life”. Each time you can consume X vitality then shoot out an energy beam with X attack power. The energy beam is a ray, When an energy beam hit an obstacle. If it’s attack power not less than defense power of obstacle, it will destroy and pass through this obstacle. Otherwise it will disappear in smoke.

Now you want to find an optimal strategy to destroy all obstacles and consume minimum vitality.

Input

The first line contain a integer T (no morn than 10), the following is T test case, for each test case :

The first line of each test case contains a integers n (1 ≤ n ≤ 300), number of obstacle.

Each of the next n lines contains 4 integers Hi, Li, Ri, Wi, (1≤Hi≤1,000,000,000, −1,000,000,000≤Li≤Ri≤1,000,000,000, 0≤Wi≤1,000,000,000) means information of obstacles.

Output

For each test case output the answer as described previously.

Sample Input

2
3
1 1 2 2
2 -1 1 4
3 -2 -1 3
3
1 -1 1 2
2 -1 1 3
3 0 2 0

Sample Output

6
3


分析:

最优选择必然是射线穿过某条线段的端点, N N 条线段,可以处理出2N个端点。
2N 2 N 个点进行极角排序去重,保证在任意区间 [i,j] [ i , j ] ,所有的点都是有序的。

定义状态:dp[i][j]—将区间 [i,j] [ i , j ] 内的所有线段射穿需要的代价
首先要明确的一点是,有效线段必须满足:该线段完全包含在 [i,j] [ i , j ] 。如果不包含,那么一条线段可能被多次射击。
其次,每个区间射击的代价,是该区间内有效线段的最大防御系数,因为这条线段必须被射击,而其它射线经过的防御系数小于它的线段,会随着它被射击而毁灭。
至此,我们已经能够确定被射击的线段是哪一条,接下来只需要枚举射击的端点,即枚举射击角度就可以了。

需要预处理是:
1. 极角排序去重 xiyjxjyi<0 x i ∗ y j − x j ∗ y i < 0 ,每个点会有新的编号
2. 判断原来的第 i i 条线段的左右端点,在排序去重后的数组的位置 Left[i] , Right[i] R i g h t [ i ] ,即以向量 (li,hi) ( l i , h i ) ,和向量 (ri,hi) ( r i , h i ) 构成的扇形包含的端点

状态转移方程:

dp[L][R]=min{dp[L][R],dp[L][k1]+dp[k+1][R]+max{wi}(LLeft[i]Right[i]R)} d p [ L ] [ R ] = m i n { d p [ L ] [ R ] , d p [ L ] [ k − 1 ] + d p [ k + 1 ] [ R ] + m a x { w i } ( L ≤ L e f t [ i ] ≤ R i g h t [ i ] ≤ R ) }

注意:
1. 计算叉积时,可能超int
2. 当区间 [L,R] [ L , R ] 内不存在可射击线段时, dp[L][R]=0 d p [ L ] [ R ] = 0


代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<time.h>
#include<iostream>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>

using namespace std;

#define bll long long
const bll inf = 1e12;
const int maxn = 610;

struct point
{
    int x,y;
    point (int n2 = 0, int n3 = 0)
    {
        x = n2, y = n3;
    }
    friend bool operator < (const point n1,const point n2)
    {
        return (bll)n1.x*n2.y < (bll)n1.y*n2.x;
    }
    friend bool operator == (const point n1,const point n2)
    {
        return (bll)n1.x*n2.y == (bll)n1.y*n2.x;
    }
}p[maxn];
bll dp[maxn][maxn];
int N,T,h[maxn],l[maxn],r[maxn],w[maxn];
int cnt,Left[maxn],Right[maxn];

void init()
{
    scanf("%d",&N);
    for (int i=0;i<N;i++)
    {
        scanf("%d %d %d %d",&h[i],&l[i],&r[i],&w[i]);
        p[i<<1] = point(l[i],h[i]);
        p[i<<1|1] = point(r[i],h[i]);
    }
    sort(p,p+2*N);

    point lastp = p[0];
    cnt = 1;
    for (int i=1;i<2*N;i++)
    {
        if (p[i] == lastp) continue;
        p[cnt++] = p[i];
    }
    for (int i=0;i<N;i++)
    {
        Left[i] = lower_bound(p,p+cnt,point(l[i],h[i]))-p;
        Right[i] = lower_bound(p,p+cnt,point(r[i],h[i]))-p;
    }
    return;
}

void solve()
{
    for (int len = 1; len <= cnt; len++)
    {
        for (int i = 0; i+len-1 < cnt; i++)
        {
            int j = i+len-1;
            int val = -1, u = -1;
            for (int k = 0; k < N; k++)
                if (Left[k] >= i && Right[k] <= j && w[k] > val)
                {
                    val = w[k];
                    u = k;
                }
            if (u == -1) dp[i][j] = 0;
            else
                for (int k = Left[u]; k <= Right[u]; k++)
                    dp[i][j] = min(dp[i][j],dp[i][k-1]+dp[k+1][j]+val);
        }
    }
    printf("%lld\n",dp[0][cnt-1]);
}

int main()
{
    scanf("%d",&T);
    while (T--)
    {
        init();
        for (int i=0;i<cnt;i++)
            for (int j=0;j<cnt;j++) 
                dp[i][j] = (i>j)? 0 : inf;
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值