結果

問題 No.115 遠足のおやつ
コンテスト
ユーザー a01sa01to
提出日時 2025-12-12 01:11:17
言語 C++23
(gcc 13.3.0 + boost 1.89.0)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 1,285 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,228 ms
コンパイル使用メモリ 285,264 KB
実行使用メモリ 12,288 KB
最終ジャッジ日時 2025-12-12 01:11:23
合計ジャッジ時間 5,082 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  int n, d, k;
  cin >> n >> d >> k;
  constexpr int INF = 1e9;
  vector dp(n + 1, vector(d + 1, vector<int>(k + 1, INF)));
  dp[0][0][0] = 0;
  for (int p = 1; p <= n; ++p) {
    for (int i = d; i >= 0; --i) {
      for (int j = k; j >= 0; --j) {
        if (dp[p - 1][i][j] == INF) continue;
        dp[p][i][j] = min(dp[p][i][j], i);
        if (i + p <= d && j < k) dp[p][i + p][j + 1] = min(dp[p][i + p][j + 1], i);
      }
    }
    // cerr << format("{}: {}\n", p, dp[p]);
  }
  if (dp[n][d][k] == INF) {
    cout << -1 << '\n';
  }
  else {
    vector<int> ans;
    pair<int, int> cur = { d, k };
    for (int x = n; x >= 0; --x) {
      // cerr << cur.first << ' ' << cur.second << ' ' << x << ' ' << dp[x][cur.first][cur.second] << '\n';
      if (cur.first != dp[x][cur.first][cur.second]) {
        auto [y, z] = cur;
        ans.push_back(y - dp[x][y][z]);
        cur = { dp[x][y][z], z - 1 };
      }
    }
    reverse(ans.begin(), ans.end());
    assert(is_sorted(ans.begin(), ans.end()));
    rep(i, k) cout << ans[i] << ' ';
    cout << '\n';
  }
  return 0;
}
0