Codeforces Round #278 (Div. 1)–A
Fight the Monster
A monster is attacking the Cyberland!
Master Yang, a braver, is going to beat the monster. Yang and the monster each have 3 attributes: hitpoints (HP), offensive power (ATK) and defensive power (DEF).
During the battle, every second the monster’s HP decrease by max(0, ATKY - DEFM), while Yang’s HP decreases by max(0, ATKM - DEFY), where index Y denotes Master Yang and index M denotes monster. Both decreases happen simultaneously Once monster’s HP ≤ 0 and the same time Master Yang’s HP > 0, Master Yang wins.
Master Yang can buy attributes from the magic shop of Cyberland: h bitcoins per HP, a bitcoins per ATK, and d bitcoins per DEF.
Now Master Yang wants to know the minimum number of bitcoins he can spend in order to win.
Input
The first line contains three integers HPY, ATKY, DEFY, separated by a space, denoting the initial HP, ATK and DEF of Master Yang.
The second line contains three integers HPM, ATKM, DEFM, separated by a space, denoting the HP, ATK and DEF of the monster.
The third line contains three integers h, a, d, separated by a space, denoting the price of 1 HP, 1 ATK and 1 DEF.
All numbers in input are integer and lie between 1 and 100 inclusively.
Output
The only output line should contain an integer, denoting the minimum bitcoins Master Yang should spend in order to win.
Sample Input
Input
1 2 1
1 100 1
1 100 100
Output
99
Input
100 100 100
1 1 1
1 1 1
Output
0
Hint
For the first sample, prices for ATK and DEF are extremely high. Master Yang can buy 99 HP, then he can beat the monster with 1 HP left.
For the second sample, Master Yang is strong enough to beat the monster, so he doesn’t need to buy anything.
题目意思:
人和怪都有3个属性,血、攻、防
人每秒减少的血量等于怪攻减去人防,怪每秒减的血量同理
然后人可以吃药加血、攻、防,给出每种药的单价,问最少花费多少钱可以杀死怪。
思路:
问题的本质是怪的HP要为零,保证人不死;
题目数据范围有暗示,所有的数都在1到100之间。考虑到怪的血、攻、防最多为100,那么人的防范围是1到100,攻的范围是200(意思是200时可以秒杀血为100,防为100的怪);然后暴力枚举。
下面贴代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
int hp_y, atk_y, def_y,hp_m, atk_m, def_m, h, a, d;
int atk1, atk2, hp1, hp2, sum, temp;
while(scanf("%d%d%d%d%d%d%d%d%d",&hp_y, &atk_y, &def_y,&hp_m, &atk_m, &def_m, &h, &a, &d)!=EOF)
{
temp = 10e9;
for(int i=atk_y;i<=201;i++){
for(int j=def_y;j<=100;j++){
sum = (i-atk_y)*a + (j - def_y)*d;
atk1 = i - def_m;
atk2 = atk_m - j;
hp1 = hp_y;
hp2 = hp_m;
if(atk1<=0) continue;
while(hp2>0&&atk2>=0){
hp1 -= atk2;
hp2 -= atk1;
if(hp1<=0){
sum += (1-hp1)*h;
hp1 = 1;
}
}
if(sum<temp)
temp = sum;
}
}
printf("%d\n",temp);
}
return 0;
}
需要注意的是:
当人的净攻击小于零时要考虑处理,怪的值为负数不需要考虑(相当于加血)
总结:
看问题看本质;
不要上来就想怎样组合hp/atk/def为最优,在这里若再把hp考虑进来,例如有hp无限大,在一定秒数内怪先死的这种想法;
或者是打打加血,再打打加防,再打打加攻这种想法(这就是我开始的想法,后面还想分析怎样最优。。。)。(为什么我就没有考虑到枚举组合呢。。我还没有考虑计算机解决问题的思想就是程序化,枚举性)
而这题只是考虑范围内攻和防的组合数(它是一定的,则不同的组合对应要加的血量也是对应的)计算机是机器,编程时考虑的是怎样写程序能包含所有的情况,并且时空复杂度最小(大牛请无视。。)