HDU5504简单思路毒瘤

个人博客链接:https://blog.nuoyanli.com/2020/04/03/hdu5504/

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=5504

题意

nn个数,选至少一个的最大乘积是多少,中间不会炸longlonglong long

思路

比赛的时候的思路:

把非00的数乘起来,
如果ansans小于00,且ansans不等于最大的负数,就输出ansans/最大负数
如果全都是0,那么就输出00,否则输出ansans

感觉这个思路没有一点问题,但是就是错了,我写wawa了十几次了吧,不想改这个思路了,求大佬hackhack

赛后acac思路:

把非00的数乘起来,

  • 如果全部为00答案为00
  • 如果只有一个负数,其他都为00答案为00
  • 剩下的就是对负数的数量讨论:
    • 如果负数是偶数个,直接输出前面乘积即可
    • 如果负数是奇数个,输出前面乘积除最大负数即可

参考代码

// 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 IOS ios::sync_with_stdio(false), cin.tie(0)
#define endl '\n'
#define kB kush_back
#define FI first
#define SE second
//#define in
#define RI register
#define m_k(a, b) make_kair(a, b)
#define debug(a) cout << "---" << a << "---" << endl
#define debug_(a, b) cout << a << "---" << b << endl
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 in
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
  solve();
  return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章