HDU 5653 (DP)

来源:互联网 发布:手机淘宝我要开店 编辑:程序博客网 时间:2024/05/18 22:42

Bomber Man wants to bomb an Array.

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 143    Accepted Submission(s): 43


Problem Description
Given an array and some positions where to plant the bombs, You have to print the Total Maximum Impact.

Each Bomb has some left destruction capability L and some right destruction capability R which means if a bomb is dropped at ith location it will destroy L blocks on the left and R blocks on the right.
Number of Blocks destroyed by a bomb is L+R+1
Total Impact is calculated as product of number of blocks destroyed by each bomb.
If ith bomb destroys Xi blocks then TotalImpact=X1X2....Xm

Given the bombing locations in the array, print the Maximum Total Impact such that every block of the array is destoryed exactly once(i.e it is effected by only one bomb).

### Rules of Bombing
1. Bomber Man wants to plant a bomb at every bombing location.
2. Bomber Man wants to destroy each block with only once.
3. Bomber Man wants to destroy every block.

 

Input
There are multi test cases denote by a integer T(T20) in the first line.

First line two Integers N and M which are the number of locations and number of bombing locations respectivly.
Second line contains M distinct integers specifying the Bombing Locations.

1 <= N <= 2000

1 <= M <= N
 

Output
as Maximum Total Impact can be very large print the floor(1000000 * log2(Maximum Total Impact)).

Hint:
Sample 1:



Sample 2:

 

Sample Input
210 20 910 30 4 8
 

Sample Output
46438565169925
 


题意:一个炸弹能够炸掉包括自己的一个连续区间内的格子,一个炸弹的威力用它炸掉的区间长度表示.n个

格子中给出m个炸弹的位置,求所有炸弹的威力之积的最大值.

由于题目要求输出的威力之积的对数,可以用乘积的对数转化为对数的和来减少精度误差.假设DP[i]表示i之

间炸完的最大价值对数和,那么DP[i] = max (DP[j]+log(j-i)/log(2)),能够转移的前提是区间[i,j-1]之间恰好存

在一个炸弹.

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <queue>using namespace std;#define maxn 2111double dp[maxn];//dp[i]表示i之前炸完的最大价值int n, m;int pos[maxn], sum[maxn];double f (int x) {    double ans = log (1.0*x)/log (2);    return ans;}int main () {    //freopen ("in.txt", "r", stdin);    int t;    scanf ("%d", &t);    while (t--) {        memset (sum, 0, sizeof sum);        scanf ("%d%d", &n, &m);        for (int i = 1; i <= m; i++) {            int x;            scanf ("%d", &x);            sum[x+1] = 1;        }        for (int i = 1; i <= n; i++) sum[i] = sum[i-1]+sum[i];        memset (dp, 0, sizeof dp);        for (int i = 1; i <= n+1; i++) {            for (int j = 1; j < i; j++)                if (sum[i-1]-sum[j-1] == 1)                    dp[i] = max (dp[i], dp[j]+f (i-j));        }        printf ("%lld\n", (long long)((floor)(1000000.0*dp[n+1])));    }    return 0;}


0 0
原创粉丝点击