HDOJ 5653 Bomber Man wants to bomb an Array.(DP)
Posted 小胡子Haso
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDOJ 5653 Bomber Man wants to bomb an Array.(DP)相关的知识,希望对你有一定的参考价值。
【HDOJ 5653】 Bomber Man wants to bomb an Array.(DP)
Bomber Man wants to bomb an Array.
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 389 Accepted Submission(s): 117
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 capabilityL
and some right destruction capability R
which means if a bomb is dropped at i th
location it will destroy L
blocks on the left and R
blocks on the right.
Number of Blocks destroyed by a bomb isL+R+1
Total Impact is calculated as product of number of blocks destroyed by each bomb.
Ifi th
bomb destroys Xi
blocks then TotalImpact=X1?X2?....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.
Each Bomb has some left destruction capability
Number of Blocks destroyed by a bomb is
Total Impact is calculated as product of number of blocks destroyed by each bomb.
If
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(T≤20)
in the first line.
First line two IntegersN
and M
which are the number of locations and number of bombing locations respectivly.
Second line containsM
distinct integers specifying the Bombing Locations.
1 <= N <= 2000
1 <= M <= N
First line two Integers
Second line contains
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:

Hint:
Sample 1:

Sample 2:

Sample Input
2 10 2 0 9 10 3 0 4 8
Sample Output
4643856 5169925
Source
Recommend
题目大意:有n个块。在其中m块上装有炸药。
要求引爆这些炸药。每个炸药可以由你给定一个左右引爆边界[L,R]表示向左L个块 向右R个块会被摧毁,即爆炸威力为L+R+1(本身所在的块也被摧毁)
设第i个炸药的爆炸威力为Xi
那么总的爆炸威力为X1*X2*X3*X4*...*Xm
问floor(1000000 * log2(Maximum Total Impact)) floor为向下取整函数 Maximum Total Impact为最大爆炸威力和
求log2就是因为成起来太大了。
利用log的性质,可知log(m,(a*b) ) = log(m,a)+log(m,b)
这样用dp[i]表示炸到第i个块可以得到的最大爆炸威力的log
这样可以枚举所有的炸药,对于每个炸药枚举爆炸范围[L,R] 枚举到左右的炸药即可
这样转移方程就是dp[R] = max(dp[R],dp[L-1]+log(R-L+1)/log2)
代码如下:
#include <iostream> #include <cmath> #include <vector> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <stack> #include <list> #include <algorithm> #include <map> #include <set> #define LL long long #define Pr pair<int,int> #define fread() freopen("in.in","r",stdin) #define fwrite() freopen("out.out","w",stdout) using namespace std; const int INF = 0x3f3f3f3f; const int msz = 10000; const int mod = 1e9+7; const double eps = 1e-8; double dp[2333]; int boom[2333]; int main() { //fread(); //fwrite(); int t,n,m; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); boom[0] = 0; boom[m+1] = n+1; for(int i = 1; i <= m; ++i) { scanf("%d",&boom[i]); boom[i]++; } sort(boom+1,boom+m+1); memset(dp,0,sizeof(dp)); for(int i = 1; i <= m; ++i) { for(int l = boom[i-1]+1; l <= boom[i]; ++l) { for(int r = boom[i+1]-1; r >= boom[i]; --r) { dp[r] = max(dp[r],dp[l-1]+log((r-l+1)*1.0)/log(2.0)); } } } LL ans = floor(1e6*dp[n]); printf("%lld\n",ans); } return 0; }
以上是关于HDOJ 5653 Bomber Man wants to bomb an Array.(DP)的主要内容,如果未能解决你的问题,请参考以下文章