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
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?
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).
#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[N][N];//在已經砍i回合並且砍了j刀的最大補刀數
int blood[N];
int cost[N];
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;
}