hdu4976 贪心+dp

A simple greedy problem.

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 458    Accepted Submission(s): 184


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
 题目大意:v用的是一个AOE英雄,每一回合对所有小兵都造成1点伤害,D用的是一个敏捷型英雄,每一回合可以选择一个小兵对他造成
1点伤害,然后问你D最多能搞死多少小兵。
思路分析:因为每一回合D最多只能搞死一只小兵,对于多个血量相同的小兵这样是极为不利的,因此需要将之前没有小兵的空余血量位进行
填充,以使得能杀死更多的小兵,d[i]指的是血量为i的小兵有多少只,c[i]指的是i血量这个位置上的小兵是由之前c[i]血量的小兵垫刀垫过来的
,进行预处理(使小兵尽量分布在不同的血量位上)然后进行dp
f[i][j]代表第i回合还剩j次补刀机会能够杀死的最多的小兵
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
using namespace std;
const int maxn=1000+10;
int d[maxn];//d[i]记录血量为i的小兵有多少只
int c[maxn];//c[i]记录当前位置的小兵是由c[i]位置的小兵垫刀垫过来的
int dp[maxn][maxn];//dp[i][j]表示第i回合还剩j次补刀机会最多能杀死多少只小兵
int kase=0;
int main()
{
    int T;
    scanf("%d",&T);
    int n,x;
    while(T--)
    {
        int ma=0;
        scanf("%d",&n);
        memset(d,0,sizeof(d));
        memset(c,0,sizeof(c));
        memset(dp,0,sizeof(dp));
        for(int i=0;i<n;i++)
        {
            scanf("%d",&x);
            d[x]++;
            ma=max(ma,x);
        }
        stack<int> q;
        int num=0;
        for(int i=1;i<=ma;i++)
        {
            if(d[i]==1)
            {
                c[i]=i;
            }
            if(d[i]==0)
            {
                q.push(i);
                num++;
            }
            if(d[i]>1)
            {
                c[i]=i;
                while(!q.empty()&&d[i]>1)
                {
                    int x=q.top();
                    if(i-x<=num)
                    {
                        q.pop();
                        c[x]=i;
                        d[i]--;
                        num--;
                    }
                    else break;
                }
            }
        }
        for(int i=1;i<=ma;i++)
    {
        for(int j=0;j<=i;j++)
        {
            if(j>0) dp[i][j]=dp[i-1][j-1];
            if(c[i]&&j+c[i]-i<i) dp[i][j]=max(dp[i][j],dp[i-1][j+c[i]-i]+1);
        }
    }
     int ans=0;
     for(int i=0;i<=ma;i++)
        {
            ans=max(ans,dp[ma][i]);
        }
     printf("Case #%d: %d\n",++kase,ans);
    }
}

 

posted on 2016-07-27 11:00  当蜗牛有了理想  阅读(463)  评论(0编辑  收藏  举报

导航