1406B - Maximum Product

贪心:

先将所有数选出最小和最大的5个数,再按绝对值大小枚举选数字

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 100010;
int n;
int a[N],b[N];
int main () {
	int T;
	cin >> T;
	while (T--) {
		cin >> n;
		for (int i = 1;i <= n;i++) cin >> a[i],b[i] = a[i];
		sort (a+1,a+1+n);
		sort (b+1,b+1+n,greater <int> ());
		LL ans = -3e18;
		for (int k = 1;k <= 5;k++) {
			LL res = 1;
			for (int i = 1;i <= k;i++) res *= b[i];
			for (int i = 1;i <= 5-k;i++) res *= a[i];
			ans = max (ans,res);
		}
		cout << ans << endl;
	}
	return 0;
}
posted @ 2022-06-18 12:31  incra  阅读(22)  评论(0)    收藏  举报