题目:​​http://acm.hdu.edu.cn/showproblem.php?pid=4310​

题目解读:可以算出每一位敌人的单位血的伤害量,进行排序,先干掉最大的,,,,

#include <stdio.h>
#include <stdlib.h>
struct hero
{
int DPS,HP;
double life;
}aaa[25];
int cmp(const void *a ,const void *b)
{
struct hero *c=(hero *)a;
struct hero *d=(hero *)b;
return c->life < d->life ? 1 : -1; //double的排序返回的一定要注意,不能直接减
}
int main()
{
int i,j,n,sum,hurt;
while(scanf("%d",&n)!=EOF)
{
sum=0;
hurt=0;
for(i=0;i<n;i++)
{
scanf("%d%d",&aaa[i].DPS,&aaa[i].HP);
aaa[i].life=1.0*aaa[i].DPS/aaa[i].HP;
sum+=aaa[i].DPS;
}
qsort(aaa,n,sizeof(aaa[0]),cmp);
for(i=0;i<n;i++)
{
for(j=1;j<=aaa[i].HP;j++)
hurt+=sum;
sum-=aaa[i].DPS;
}
printf("%d\n",hurt);
}
return 0;
}
还有一种方法是:状态压缩dp,用dp[mask]表示杀死mask集合的敌人时,这些敌人造成的最小hp消耗。
有转移方程dp[mask] = min{dp[mask - {i}] + hp_sum[mask] * dps[i], for all i

#include <cstdio>
#include <cstring>const int MAXN = 20;
struct SS {int dps, hp;} a[MAXN];
int sum[1<<MAXN], dp[1<<MAXN];
int n;int main() {
// freopen("data.in","r",stdin);
// freopen("data.out","w",stdout);
while (scanf("%d",&n) != EOF) {
for (int i = 0 ; i < n ; i++)
scanf("%d%d",&a[i].dps,&a[i].hp);
memset(sum, 0, sizeof(sum));
for (int mask = 0 ; mask < (1<<n) ; ++mask)
for (int i = 0 ; i < n ; i++)
sum[mask] += a[i].hp;
memset(dp, 0x3f, sizeof(dp));
dp[0] = 0;
for (int mask = 0 ; mask < (1<<n) ; ++mask) {
for (int i = 0 ; i < n ; i++) {
if (mask & (1<<i)) continue;
int tmask = mask + (1<<i);
int cost = dp[mask] + sum[tmask] * a[i].dps;
if (cost < dp[tmask]) dp[tmask] = cost;
}
}
printf("%d\n", dp[(1<<n)-1]);
}
return 0;
}