文章
68
粉丝
691
获赞
26
访问
578.3k
这道题主要考思维吧,对正数而言从大到小两两配对,1单独统计,因为1*5<1+5,1应该单独相加。0单独统计,对正数而言0是无用的,对负数而言,从小到大两两配对,如果有落单的负数,则用零抵消,没有零就只能相减。
#include<iostream>
#include<vector>
#include<map>
#include<queue>
#include<string>
#include<string.h>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll int
#define inf 0x3ffffff
#define vec vector<ll>
#define MAX 105
int main() {
ll N, n, a[MAX]; cin >> N;
for (int i = 0; i < N; i++) {
cin >> n; vec v1, v2; ll cnt0 = 0, cnt1 = 0, sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] > 1)v1.push_back(a[i]);
else if (a[i] < 0)v2.push_back(a[i]);
else if (a[i] == 0)cnt0++;
else cnt1++;//1也要单独统计
}
sort(v1.begin(), v1.end());
sort(v2.begin(), v2.end());
for (ll i = v1.size() - 2; i >= 0; i -= 2)
sum += v1[i] * v1[i + 1];
if (v1.size() % 2 == 1)
sum += v1[0];
for (ll i = 1; i < v2.s...
登录后发布评论
暂无评论,来抢沙发