BestCoder – ntzyz's blog https://archive.ntzyz.io Wed, 30 Mar 2016 08:23:42 +0000 zh-CN hourly 1 https://wordpress.org/?v=5.8 [HDU 5504] GT and sequence https://archive.ntzyz.io/2015/10/28/hdu-5504-gt-and-sequence/ https://archive.ntzyz.io/2015/10/28/hdu-5504-gt-and-sequence/#comments Wed, 28 Oct 2015 13:48:10 +0000 https://blog.dimension.moe/?p=241 继续阅读[HDU 5504] GT and sequence]]> 题目好像不难欸,思路很清晰的……但是WA了好久……(摔
题目链接:Problem – 5504
思路很简单,既然题目保证了不会溢出,那就开了long long直接上。
对于所有正数,全部直接乘绝对是最优解。
对于偶数个负数,全部相乘后负号自然就都没了,还是全部乘上去。
最后,奇数个负数,那么就去掉绝对值最小的负数,然后将余下的偶数个奇数全部相乘即可。

这个思路是没问题的是个人都想得到,但是实际写的时候问题颇多。此题出自Bestcoder,当时情况是这样的:(摔

Accepts: 95   Submissions: 1467

_(:з」∠)_

不许吐槽代码里的LoveLive!

代码:

[cc lang=”cpp”]#include

using namespace std;

typedef long long LoveLive;

int main() {
ios::sync_with_stdio(false);
int T;
cin >> T;
while (T–) {
int n;
cin >> n;
vector la, lb;
LoveLive result = 1;
for (int i = 0; i != n; i++) {
LoveLive temp;
cin >> temp;
if (temp > 0)
la.push_back(temp);
else if (temp < 0) lb.push_back(temp); } for (auto i: la) result *= i; if (lb.size() == 0 && la.size() == 0) { cout << 0 << endl; continue; } if (lb.size() == 1 && la.size() == 0) { if (n == 1) { cout << lb[0] << endl; continue; } else { cout << 0 << endl; continue; } } if (lb.size() & 1) { sort(lb.begin(), lb.end()); lb.pop_back(); } for (auto i: lb) result *= i; if (la.size() + lb.size() == n) { cout << result << endl; continue; } else cout << max(static_cast(0), result) << endl; } return 0; } [/cc]

]]>
https://archive.ntzyz.io/2015/10/28/hdu-5504-gt-and-sequence/feed/ 1