题目链接:[http://acm.hdu.edu.cn/showproblem.php?pid=6365]
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.
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.
For each test case output the answer as described previously.
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
6
3
最优选择必然是射线穿过某条线段的端点, N N 条线段,可以处理出 2N 2 N 个端点。
对 2N 2 N 个点进行极角排序去重,保证在任意区间 [i,j] [ i , j ] ,所有的点都是有序的。
定义状态:dp[i][j]—将区间 [i,j] [ i , j ] 内的所有线段射穿需要的代价
首先要明确的一点是,有效线段必须满足:该线段完全包含在 [i,j] [ i , j ] 内。如果不包含,那么一条线段可能被多次射击。
其次,每个区间射击的代价,是该区间内有效线段的最大防御系数,因为这条线段必须被射击,而其它射线经过的防御系数小于它的线段,会随着它被射击而毁灭。
至此,我们已经能够确定被射击的线段是哪一条,接下来只需要枚举射击的端点,即枚举射击角度就可以了。
需要预处理是:
1. 极角排序去重 xi∗yj−xj∗yi<0 x i ∗ y j − x j ∗ y i < 0 ,每个点会有新的编号
2. 判断原来的第 i i 条线段的左右端点,在排序去重后的数组的位置 Left[i] L e f t [ i ] , Right[i] R i g h t [ i ] ,即以向量 (li,hi) ( l i , h i ) ,和向量 (ri,hi) ( r i , h i ) 构成的扇形包含的端点
状态转移方程:
注意:
1. 计算叉积时,可能超int
2. 当区间 [L,R] [ L , R ] 内不存在可射击线段时, dp[L][R]=0 d p [ L ] [ R ] = 0
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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;iscanf("%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;ireturn;
}
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;ifor (int j=0;jj)? 0 : inf;
solve();
}
return 0;
}