HDU 6365 Shoot Game(区间dp+离散化)

Problem 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

Hint

The first testcase as shown in the picture:

题意:要求打穿所有的木板并使得消耗最小。

思路:最优策略必然是考虑从每个板子的两个端点开枪,因为这样可以覆盖更多的区间。

所以可以发现消耗量的多少其实取决于每个木板之间的端点关系,也就是说消耗量与两端有关,并且n的范围为<300,n^3的复杂度可以接受,所以考虑区间dp;

状态转移:dp[i][j]表示从i到j的最小消耗量,转移应该是找到这个区间的最大消耗的木板,考虑从左边打,还是右边打。

dp[i][j]=min(dp[i][k-1]+dp[k+1][j]+mx,dp[i][j]);

但是i,j的长度太大,同时我们只需要考虑端点的关系,所以离散化处理就好。

离散化的方法可以之间按于原点连线的斜率大小来排序。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
#define inf 0x3f3f3f3f
int n,hn;
ll dp[600][600];
struct Point {
    int x, y;
    Point() {}
    Point(int x, int y) : x(x), y(y) {}
    bool operator == (const Point& c) const {
        return 1ll * x * c.y - 1ll * y * c.x == 0;
    }
    bool operator < (const Point& c) const {
        return 1ll * x * c.y - 1ll * y * c.x < 0;
    }
} p[maxn];
int h[maxn],l[maxn],r[maxn],w[maxn];
int Find(Point x)
{
    return lower_bound(p + 1, p + 1 + hn, x) - p;
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d%d",&h[i],&l[i],&r[i],&w[i]);
            p[2*i-1]=Point(l[i],h[i]);
            p[2*i]=Point(r[i],h[i]);
        }
        sort(p+1,p+2*n+1);
        hn=unique(p+1,p+2*n+1)-p-1;
        for(int i=1;i<=n;i++)
        {
            l[i]=Find(Point(l[i],h[i]));
            r[i]=Find(Point(r[i],h[i]));
            //printf("%d %d\n",l[i],r[i]);
        }
        for(int len=1;len<=hn;len++)
        {
            for(int L=1;L+len-1<=hn;L++)
            {
                int R=L+len-1;
                dp[L][R]=(1ll<<60);
                int mx=-1,mv;
                for(int k=1;k<=n;k++)//找到这个区间的需要最大消耗
                {
                    if(l[k]>=L&&r[k]<=R)
                    {
                        if(w[k]>mx)
                        {
                            mx=w[k];
                            mv=k;
                        }
                    }
                }
                if(mx==-1) dp[L][R]=0;
                else
                {
                    int s=l[mv],e=r[mv];
                    for(int k=s;k<=e;k++)
                    {
                        dp[L][R]=min(dp[L][R],dp[L][k-1]+dp[k+1][R]+mx);
                    }
                }
            }
        }
        cout<<dp[1][hn]<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/81515944