結果

問題 No.3409 How Many Gift Boxes?
コンテスト
ユーザー tnakao0123
提出日時 2025-12-17 19:24:26
言語 C++17
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 2,506 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 344 ms
コンパイル使用メモリ 48,104 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-12-17 21:19:42
合計ジャッジ時間 4,195 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:72:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   72 |   scanf("%d%d", &h, &w);
      |   ~~~~~^~~~~~~~~~~~~~~~
main.cpp:73:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   73 |   for (int i = 0; i < h; i++) scanf("%d", as + i);
      |                               ~~~~~^~~~~~~~~~~~~~
main.cpp:74:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   74 |   for (int i = 0; i < w; i++) scanf("%d", bs + i);
      |                               ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #
raw source code

/* -*- coding: utf-8 -*-
 *
 * 3409.cc:  No.3409 How Many Gift Boxes? - yukicoder
 */

#include<cstdio>
#include<algorithm>

using namespace std;

/* constant */

const int MAX_H = 100000;
const int MAX_W = 100000;
const int MAX_K = MAX_H + MAX_W;
const int MOD = 1000000007;

/* typedef */

using ll = long long;

template<const int MOD>
struct MI {
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) { if (v < 0) v += MOD; }
  MI(long long _v): v(_v % MOD) { if (v < 0) v += MOD; }

  explicit operator int() const { return v; }
  
  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator-() const { return MI(MOD - v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  bool operator==(const MI m) const { return v == m.v; }
  bool operator!=(const MI m) const { return v != m.v; }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }
};

using mi = MI<MOD>;

/* global variables */

int as[MAX_H], bs[MAX_W];
int uas[MAX_K], acs[MAX_K], bcs[MAX_K];
ll ass[MAX_H + 1];

/* subroutines */

/* main */

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  for (int i = 0; i < h; i++) scanf("%d", as + i);
  for (int i = 0; i < w; i++) scanf("%d", bs + i);

  mi minsum = 0, maxsum = 0;

  // minsum
  int k = 0;
  for (int i = 0; i < h; i++) uas[k++] = as[i];
  for (int i = 0; i < w; i++) uas[k++] = bs[i];
  sort(uas, uas + k);
  k = unique(uas, uas + k) - uas;
  
  for (int i = 0; i < h; i++) {
    int ai = lower_bound(uas, uas + k, as[i]) - uas;
    acs[ai]++;
  }
  for (int i = 0; i < w; i++) {
    int bi = lower_bound(uas, uas + k, bs[i]) - uas;
    bcs[bi]++;
  }

  for (int i = 0; i < k; i++)
    minsum += (ll)uas[i] * max(acs[i], bcs[i]);
  
  // maxsum
  sort(as, as + h);
  for (int i = 0; i < h; i++) ass[i + 1] = ass[i] + as[i];
  
  for (int i = 0; i < w; i++) {
    int j = lower_bound(as, as + h, bs[i]) - as;
    maxsum += ass[j] + (ll)(h - j) * bs[i];
  }

  printf("%d\n%d\n", (int)minsum, (int)maxsum);
  
  return 0;
}

0