結果

問題 No.1467 Selling Cars
ユーザー 👑 emthrmemthrm
提出日時 2021-04-05 23:02:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,553 ms / 4,000 ms
コード長 2,561 bytes
コンパイル時間 2,353 ms
コンパイル使用メモリ 209,148 KB
実行使用メモリ 4,892 KB
最終ジャッジ日時 2023-08-30 10:30:24
合計ジャッジ時間 28,554 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 631 ms
4,776 KB
testcase_01 AC 559 ms
4,684 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1,551 ms
4,380 KB
testcase_04 AC 1,552 ms
4,500 KB
testcase_05 AC 1,535 ms
4,476 KB
testcase_06 AC 1,545 ms
4,384 KB
testcase_07 AC 1,543 ms
4,408 KB
testcase_08 AC 1,553 ms
4,384 KB
testcase_09 AC 3 ms
4,436 KB
testcase_10 AC 3 ms
4,640 KB
testcase_11 AC 1,007 ms
4,384 KB
testcase_12 AC 1,281 ms
4,472 KB
testcase_13 AC 1,116 ms
4,504 KB
testcase_14 AC 161 ms
4,380 KB
testcase_15 AC 525 ms
4,464 KB
testcase_16 AC 300 ms
4,380 KB
testcase_17 AC 802 ms
4,380 KB
testcase_18 AC 1,132 ms
4,468 KB
testcase_19 AC 579 ms
4,628 KB
testcase_20 AC 470 ms
4,892 KB
testcase_21 AC 634 ms
4,668 KB
testcase_22 AC 556 ms
4,740 KB
testcase_23 AC 502 ms
4,668 KB
testcase_24 AC 449 ms
4,840 KB
testcase_25 AC 83 ms
4,376 KB
testcase_26 AC 748 ms
4,640 KB
testcase_27 AC 527 ms
4,380 KB
testcase_28 AC 326 ms
4,456 KB
testcase_29 AC 226 ms
4,380 KB
testcase_30 AC 221 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
constexpr int INF = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr double EPS = 1e-8;
constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
constexpr int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
constexpr int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << fixed << setprecision(20);
  }
} iosetup;

int main() {
  int m, n; cin >> m >> n;
  vector<int> a(m), b(n);
  REP(i, m) cin >> a[i];
  sort(ALL(a));
  REP(i, n) cin >> b[i];
  sort(ALL(b));
  vector<int> closest(m);
  REP(i, m) {
    closest[i] = lower_bound(ALL(b), a[i]) - b.begin();
    if (closest[i] == n || (closest[i] > 0 && a[i] - b[closest[i] - 1] <= b[closest[i]] - a[i])) --closest[i];
  }
  for (int k = 1; k <= m; ++k) {
    ll ans = 0;
    vector<deque<int>> used(n);
    REP(i, m) {
      if (used[closest[i]].size() < k) {
        used[closest[i]].emplace_back(i);
        ans += abs(b[closest[i]] - a[i]);
      } else {
        int l = closest[i] - 1, r = closest[i] + 1;
        while (l >= 0 && used[l].size() == k) --l;
        while (r < n && used[r].size() == k) ++r;
        ll l_cost = LINF, r_cost = LINF;
        if (l >= 0) {
          l_cost = 0;
          FOR(j, l, r) {
            if (j + 1 < n && !used[j + 1].empty()) {
              l_cost -= abs(b[j + 1] - a[used[j + 1].front()]);
              l_cost += abs(b[j] - a[used[j + 1].front()]);
            }
          }
          l_cost += abs(b[r == n || used[r].empty() ? r - 1 : r]  - a[i]);
        }
        if (r < n) r_cost = abs(b[r] - a[i]);
        if (l_cost <= r_cost) {
          ans += l_cost;
          FOR(j, l, r) {
            if (j + 1 < n && !used[j + 1].empty()) {
              used[j].emplace_back(used[j + 1].front());
              used[j + 1].pop_front();
            }
          }
          used[r == n || used[r].empty() ? r - 1 : r].emplace_back(i);
        } else {
          ans += r_cost;
          used[r].emplace_back(i);
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
0