結果

問題 No.3372 Suitable Constraint
コンテスト
ユーザー tnakao0123
提出日時 2025-11-23 17:38:18
言語 C++17
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 20 ms / 2,000 ms
+ 758µs
コード長 1,298 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 289 ms
コンパイル使用メモリ 70,188 KB
実行使用メモリ 5,888 KB
最終ジャッジ日時 2026-07-16 22:47:31
合計ジャッジ時間 2,387 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

/* -*- coding: utf-8 -*-
 *
 * 3372.cc:  No.3372 Suitable Constraint - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

using ll = long long;
using vi = vector<int>;
using pii = pair<int,int>;

/* global variables */

int as[MAX_N];

/* subroutines */

pii minmaxv(vi &bs) {
  int minb = *min_element(bs.begin(), bs.end());
  int maxb = *max_element(bs.begin(), bs.end());
  return {minb, maxb};
}

/* main */

int main() {
  int tn;
  scanf("%d", &tn);

  while (tn--) {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d", as + i);

    int zc = 0;
    vi pas, mas;
    for (int i = 0; i < n; i++) {
      if (as[i] > 0) pas.push_back(as[i]);
      else if (as[i] < 0) mas.push_back(as[i]);
      else zc++;
    }

    ll x = 0;
    if (zc > 0) x = 0;
    else if (mas.empty()) {
      auto [minp, maxp] = minmaxv(pas);
      x = (ll)minp * maxp;
    }
    else if (pas.empty()) {
      auto [minm, maxm] = minmaxv(mas);
      x = (ll)minm * maxm;
    }
    else {
      int minp = *min_element(pas.begin(), pas.end());
      int maxm = *max_element(mas.begin(), mas.end());
      x = (ll)minp * maxm;
    }

    printf("%lld\n", x);
  }

  return 0;
}

0