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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章