HDU5504简单思路毒瘤
原创
©著作权归作者所有:来自51CTO博客作者nuoyanli的原创作品,请联系作者获取转载授权,否则将追究法律责任
个人博客链接:https://blog.nuoyanli.com/2020/04/03/hdu5504/
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=5504
题意
给个数,选至少一个的最大乘积是多少,中间不会炸。
思路
比赛的时候的思路:
把非的数乘起来,
如果小于,且不等于最大的负数,就输出/最大负数
如果全都是0,那么就输出,否则输出
感觉这个思路没有一点问题,但是就是错了,我写了十几次了吧,不想改这个思路了,求大佬。
赛后思路:
把非的数乘起来,
- 如果全部为答案为
- 如果只有一个负数,其他都为答案为
- 剩下的就是对负数的数量讨论:
- 如果负数是偶数个,直接输出前面乘积即可
- 如果负数是奇数个,输出前面乘积除最大负数即可
参考代码
// nuoyanli
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
//#include <bits/stdc++.h>
using namespace std;
#define
#define
#define
#define
#define
//#define in
#define
#define
#define
#define
const double pi = acos(-1.0);
const double eps = 1e-9;
typedef long long LL;
const int N = 1e5 + 10;
const int M = 1e3 + 10;
const int mod = 2015;
const LL inf = 1e18;
const double f = 2.32349;
int a[N], b[N];
void solve() {
IOS;
int t, k = 0;
cin >> t;
while (t--) {
int n;
cin >> n;
int tot = n, tot_0 = 0, tot_f = 0;
LL ans = 1, Max =-inf;
while (n--) {
LL x;
cin >> x;
if (x < 0) {
Max = max(Max, x);
tot_f++;
}
if (x != 0) {
ans *= x;
} else {
tot_0++;
}
}
if (tot_f & 1) {
if (tot_f == 1) {
if (tot_f + tot_0 == tot && tot_0 != 0) {
ans = 0;
} else if (tot_f != tot && tot_f + tot_0 != tot) {
ans = ans / Max;
}
} else {
ans = ans / Max;
}
} else if (tot == tot_0) {
ans = 0;
}
cout << ans << endl;
}
}
signed main() {
#ifdef
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
solve();
return 0;
}