Probelm 地址:http://acm.hdu.edu.cn/showproblem.php?pid=5504
Thinking about it:
如果有0的话,判断起来会比没有0麻烦一点.所以先思考没有0 的解决方法。
如果没有0,那么可以得到来嗯个序列,整数序列,和负数序列,显然整数序列的所有值都应乘起来,而负数应该从小到大取奇数个。这里会有一个特殊情况:只有一个负数,而无正数,此时应该取一个负数。此时我们已经初步得到了一个答案。
在考虑有0的情况,其实此时就简单了很多,因为只有把上面的答案与0比较大小就好了。这样子,也同时很好地处理了存在0且无整数也只有一个负数的情况。
还有一个情况就是只有0,这是当然也取0;
Code:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 62 + 5;
typedef long long LL;
typedef unsigned long long uLL;
std::vector<LL> positive, negetive;
int N;
void read() {
positive.clear();
negetive.clear();
cin >> N;
LL t;
for (int i = 0; i < N; ++i) {
cin >> t;
if (t > 0) {
positive.push_back(t);
} else if (t < 0) {
negetive.push_back(t);
}
}
}
LL get_pn() {
if (negetive.size() == 1 && positive.size() == 0) {
return negetive[0];
}
if ((int)negetive.size() + (int)positive.size() == 0) {
return 0;
}
LL ans = 1;
for (int i = 0; i < (int)positive.size(); ++i) {
ans *= positive[i];
}
if (negetive.size() & 1) {
sort(negetive.begin(), negetive.end());
negetive.pop_back();
}
for (int i = 0; i < (int) negetive.size(); ++i) {
ans *= negetive[i];
}
return ans;
}
LL work() {
read();
LL value = get_pn(); // without 0
if ((int)negetive.size() + (int)positive.size() < N) {
value = value > 0 ? value : 0;
}
return value;
}
int main(int argc, char const *argv[]) {
ios::sync_with_stdio(false);
cin.tie(0);
int T;
cin >> T;
while (T --) {
cout << work() << endl;
}
return 0;
}
原文:http://www.cnblogs.com/Emerald/p/4889409.html