google APAC round 2 problem B New Years Eve 倒酒问题

这道题的做法:分层,一层层递推计算,如果计算到有一层酒杯没有溢出则可以退出循环,下面所有酒杯必定为空。注意不用开很大的三维数组,两层二维数组足矣,在循环时用mod2的方法决定当前位置在三维数组的哪一层

#include <iostream>
#include <iomanip>
using namespace std;
double arr[2][400][400];
int main() {
	freopen("B-large-practice.in","r",stdin);
	freopen("output.txt","w",stdout);
	int T,B,L,N;
	bool overflow;
	cin >> T;

	for (int t = 1; t <= T; t++) {
		cin >> B >> L >> N;
		arr[0][0][0] = 750 * B;
		overflow = B >= 1;
		int l;
		for (l = 1; l < L && overflow; l++) {
			overflow = false;
			memset(arr[l%2],0,400*400);
			for (int i = 0; i < l; i++) {
				for (int j = 0; j <= i; j++) {
					if (arr[(l-1)%2][i][j] > 250) {
						double tmp = (arr[(l-1)%2][i][j] - 250) / 3;
						arr[l%2][i][j] += tmp;
						arr[l%2][i+1][j] += tmp;
						arr[l%2][i+1][j+1] += tmp;
					}
				}
			}
			for (int i = 0; i <= l; i++) {
				for (int j = 0; j <= i; j++) {
					if (arr[l%2][i][j] > 250) {
						overflow = true;
					}
				}
			}
		}
		if (l < L) {
			cout << "Case #" << t << ": 0.0000000" << endl;
		} else {
			int i, j;
			for (i = 1; i < N; N -= i, i++);
			j = N;
			cout << "Case #" << t << ": " << fixed << setprecision(7) << (arr[(L-1)%2][i-1][j-1] > 250.0 ? 250.0 : arr[(L-1)%2][i-1][j-1]) << endl;
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值