codeforces 487A A. Fight the Monster(二分)

时间:2023-03-09 17:19:28
codeforces 487A A. Fight the Monster(二分)

题目链接:

A. Fight the Monster

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 bymax(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 HPa 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 HPATK and DEF of Master Yang.

The second line contains three integers HPM, ATKM, DEFM, separated by a space, denoting the HPATK 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.

Examples
input
1 2 1
1 100 1
1 100 100
output
99
input
100 100 100
1 1 1
1 1 1
output
0

题意:

现在打怪物,人有血,攻击力,防御力,怪物也是,只要人还有血,怪物没血,那么就赢了,现在买一点血需h,一点攻击力需a,一点防御力需d,求要获胜需要的最少花费;

思路:

二分答案,然后枚举各买多少血,攻击力和防御力,然后判断是否能胜,就这样找到最小花费;

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e5+20;
const int maxn=4e3+220;
const double eps=1e-12;
int Hy,Ay,Dy,Hm,Am,Dm,h,a,d;
inline int ok(int sa,int aa,int sb,int bb)
{
if(aa==0)return 0;
if(bb==0)return 1;
int tempa=sa/bb;
if(tempa*bb==sa)tempa--;
if(sb-tempa*aa<=0)return 1;
return 0;
}
inline int check(int co)
{
for(int x=0;x*h<=co;x++)
{
for(int y=0;x*h+y*a<=co;y++)
{
for(int z=0;x*h+y*a+z*d<=co;z++)
{
if(ok(x+Hy,max(0,y+Ay-Dm),Hm,max(0,Am-Dy-z)))return 1;
}
}
}
return 0;
} int main()
{ read(Hy);read(Ay);read(Dy);
read(Hm);read(Am);read(Dm);
read(h);read(a);read(d);
int l=0,r=3e4+10;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid))r=mid-1;
else l=mid+1;
}
cout<<r+1<<endl; return 0;
}