[hdu4976]貪心+dp

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[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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章