NOIP模拟 Ball - log积化和

题目描述

Alice 与 Bob 在玩游戏。他们一共玩了 t 轮游戏。游戏中,他们分别获得了 n 个和 m 个小球。每个球上有一个分数。每个人的得分都为他所获得所有小球分数的乘积,分数小者获胜。问每轮游戏谁会获胜?请输出每轮游戏的胜者。数据保证不会出现平局,且两个人分数差异大于任意一个人分数的 1% 。

输入格式

第一行为两人玩的轮数 t(1≤t≤10)。
每一轮游戏的输入中:
第一行一个整数 n,代表 Alice 获得球的个数。
第二行为 n 个整数 ai,代表 Alice 每个球的分数。
第三行一个整数 m,代表 Bob 获得球的个数。
第四行为 m 个整数 bi,代表 Bob 每个球的分数。

输出格式

输出共 t 行,每行为该轮胜者的名字“Alice”或“Bob”。

样例数据 1

输入  [复制]

1
3
2 3 4
4
1 3 4 5

输出

Alice

备注

【样例说明】

Alice:2 * 3 * 4 = 24
Bob: 1 * 3 * 4 * 5 = 60

【数据范围】

对于 40% 的数据:n,m,ai,bi≤10;
对于 100% 的数据:1≤n,m≤100000;-10000≤ai,bi≤10000

题目分析

在精度要求不高的情况下,可以将求积用log化为求和a * b = log(a) + log(b).
此题迎刃而解。

code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;

const int N = 100005, S = 45000;
int t, n, m;
double a, b;
typedef long long ll;

//#define endll putchar('\n')
inline int read(){
	int i = 0, f = 1; char ch = getchar();
	for(; (ch < '0' || ch > '9') && ch != '-'; ch = getchar());
	if(ch == '-') f = -1, ch = getchar();
	for(; ch >= '0' && ch <= '9'; ch = getchar())
		i = (i << 3) + (i << 1) + (ch - '0');
	return i * f;
}

inline void wr(ll x){	
	if(x < 0) x = -x, putchar('-');
	if(x > 9) wr(x / 10);
	putchar(x % 10 + '0');
}

int main(){
//	freopen("ball.in", "r", stdin);
//	freopen("ball.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(NULL), cout.tie(NULL);
	t = read();
	while(t--){
		double fn, fm; fn = fm = 1;
		a = b = 1;
		n = read();
		for(int i = 1; i <= n; i++){
			double x = 1.0 * read();
			if(fn == 0) continue;
			if(x == 0){
				fn = 0;
				continue;
			}
			if(x < 0) fn *= -1, x = -x;
			a = a + log(x);
		}
		m = read();
		for(int i = 1; i <= m; i++){
			double x = 1.0 * read();
			if(fm == 0) continue;
			if(x == 0){
				fm = 0;
				continue;
			}
			if(x < 0) fm *= -1, x = -x;
			b = b + log(x);
		}
//		cout << fn * a << " "<< fm * b;
		if(fn * a < fm * b) cout << "Alice" << endl;
		else cout << "Bob" << endl;
	}
	return 0;
}
posted @ 2017-10-08 16:46  CzYoL  阅读(233)  评论(1编辑  收藏  举报