其他分享
首页 > 其他分享> > 1406B - Maximum Product

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;
}

标签:sort,Product,int,LL,1406B,Maximum,long,cin,include
来源: https://www.cnblogs.com/incra/p/16388104.html