Codeforces Round #278 (Div. 2) C. Fight the Monster 二分+枚举

二分所需的花费,对给定花费枚举hp、atk、def的购买数量。

代码如下

#include <cstdio>
using namespace std;
#define INF 30100
#define max(a, b) (a) > (b) ? (a) : (b)
int hp[2], atk[2], def[2];
int h, a, d;

bool check(int x){
	int bound_hp = x / h, rest = x;
	bool found = false;
	for(int i = 0; i <= bound_hp && !found; ++i){
		int bound_atk = rest / a, rest_d = rest, new_hp = hp[0] + i;
		for(int j = 0; j <= bound_atk && !found; ++j){
			int new_atk = atk[0] + j;
			int new_def = def[0] + rest_d / d;
			rest_d -= a;
			int dec_y = max(0, atk[1] - new_def), dec_m = max(0, new_atk - def[1]);
			if(dec_m == 0)
				continue;
			else if(dec_y == 0)
				found = true;
			else{
				int time = hp[1] % dec_m ? hp[1] / dec_m + 1 : hp[1] / dec_m;
				if(dec_y * time < new_hp)
					found = true;
			}
		}
		rest -= h;
	}
	return found;
}

int main(){
	scanf("%d %d %d", &hp[0], &atk[0], &def[0]);
	scanf("%d %d %d", &hp[1], &atk[1], &def[1]);
	scanf("%d %d %d", &h, &a, &d);
	int l = 0, r = INF;
	while(l < r){
		int mid = l + ((r - l) >> 1);
		if(check(mid))
			r = mid;
		else
			l = mid + 1;
	}
	printf("%d\n", l);
	return 0;
}


你可能感兴趣的:(codeforces,二分)