您的位置:首页 > 其它

[hdu4976]贪心+dp

2014-08-22 16:52 218 查看


A simple greedy problem.

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 71    Accepted Submission(s): 30


Problem Description



Victor and Dragon are playing DotA. Bored of normal games, Victor challenged Dragon with a competition of creep score (CS). In this competition, there are N enemy creeps for them. They hit the enemy one after another and Dragon takes his turn first. Victor
uses a strong melee character so that in his turn, he will deal 1 damage to all creeps. Dragon uses a flexible ranged character and in his turn, he can choose at most one creep and deal 1 damage. If a creep take 1 damage, its health will reduce by 1. If a
creep’s current health hits zero, it dies immediately and the one dealt that damage will get one score. Given the current health of each creep, Dragon wants to know the maximum CS he can get. Could you help him?

 

Input

The first line of input contains only one integer T(<=70), the number of test cases. 

For each case, the first line contains 1 integer, N(<=1000), indicating the number of creeps. The next line contain N integers, representing the current health of each creep(<=1000).

 

Output

Each output should occupy one line. Each line should start with "Case #i: ", with i implying the case number. For each case, just output the maximum CS Dragon can get.

 

Sample Input

2
5
1 2 3 4 5
5
5 5 5 5 5

 

Sample Output

Case #1: 5
Case #2: 2
题目大意:一堆怪,A能砍一片,每个1滴血,B砍一个,一个1血,B先砍,问B最大补刀数构造血量种类尽可能多的怪,记录花费,这样怪的血量从小到大就会递减,每次存在砍不砍的决策,DP即可
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 1005
int dp

;//在已经砍i回合并且砍了j刀的最大补刀数
int blood
;
int cost
;
int cnt = 1;
int main()
{
#ifdef DeBUGs
freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
int T;
scanf("%d", &T);
while (T--)
{
int n, x;
scanf("%d", &n);
int maxblood = 0;
memset(blood, 0, sizeof(blood));
memset(cost, 0, sizeof(cost));
memset(dp, -1, sizeof(dp));
for (int i = 0; i < n; i++)
{
scanf("%d", &x);
blood[i] = x;
maxblood = max(maxblood, x);
}
sort(blood, blood + n);
//构造血量种类尽可能多的怪物并计算花费(需要多少刀砍死)
for (int j = 0; j < n; j++)
{
int b = blood[j];
int val = 1;
while (cost[b] && b >= 1)//出现一个为0的即补上
{
b--;
val++;
}
if (b > 0)
cost[b] = val;
}
// for (int i = 0; i < 10; i++)
// {
//     printf("%d ", cost[i]);
// }
// printf("\n");
dp[0][0] = 0;
for (int i = 1; i <= maxblood; i++)
{
for (int j = 0; j <= i; j++)
{
if ( j - 1 >= 0)
dp[i][j] = max(dp[i][j], dp[i - 1][j - 1]);
if (cost[i] && j + cost[i] - 1 <= i )
dp[i][j] = max(dp[i][j], dp[i - 1][j + cost[i] - 1] + 1);
//两种转移方式等价,状态的转移可以通过约束与能否转移实现
// if ( j - 1 >= 0  && dp[i - 1][j - 1] != -1)
//     dp[i][j] = max(dp[i][j], dp[i - 1][j - 1]);
// if (cost[i]  && dp[i - 1][j + cost[i] - 1] != -1)
//     dp[i][j] = max(dp[i][j], dp[i - 1][j + cost[i] - 1] + 1);
}
}
// for (int i = 0; i <= maxblood; i++)
// {
//     for (int j = 0; j <= maxblood; j++)
//     {
//         printf("%d ", dp[i][j]);
//     }
//     printf("\n");
// }
int ans = -1;
for (int i = 0; i <= maxblood; i++)
{
ans = max(ans, dp[maxblood][i]);
}
printf("Case #%d: %d\n", cnt++, ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: