結果

問題 No.838 Noelちゃんと星々3
ユーザー pekempeypekempey
提出日時 2019-06-14 21:47:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 1,301 bytes
コンパイル時間 2,080 ms
コンパイル使用メモリ 176,360 KB
実行使用メモリ 12,764 KB
最終ジャッジ日時 2024-04-26 15:21:53
合計ジャッジ時間 3,730 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
12,632 KB
testcase_01 AC 91 ms
12,572 KB
testcase_02 AC 95 ms
12,632 KB
testcase_03 AC 91 ms
12,632 KB
testcase_04 AC 89 ms
12,764 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (int)(n) - 1; i >= 0; i--)
using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> a(n);
  map<ll, int> mp;
  rep(i, n) cin >> a[i], mp[a[i]]++;
  vector<pair<ll, int>> b;
  for (auto e : mp) {
    if (e.second >= 3) {
      b.emplace_back(e.first, 1);
      b.emplace_back(e.first, e.second - 2);
      b.emplace_back(e.first, 1);
    } else if (e.second == 2) {
      b.emplace_back(e.first, 1);
      b.emplace_back(e.first, 1);
    } else {
      b.emplace_back(e.first, 1);
    }
  }
  vector<ll> dp(b.size() + 1, 1e18);
  dp[0] = 0;
  for (int i = 0; i < b.size(); i++) {
    int cnt = 0;
    for (int j = i + 1; j <= min<int>(b.size(), i + 10); j++) {
      cnt += b[j - 1].second;
      int m = 0;
      int mid = 0;
      for (int k = i; k < j; k++) {
        m += b[k].second;
        if (m * 2 >= cnt) {
          mid = k;
          break;
        }
      }
      ll cost = 0;
      for (int k = i; k < j; k++) {
        cost += abs(b[mid].first - b[k].first) * b[k].second;
      }
      if (cnt >= 2) {
        dp[j] = min(dp[j], dp[i] + cost);
      }
    }
  }
  cout << dp[b.size()] << endl;
}
0