結果
問題 | No.1861 Required Number |
ユーザー |
|
提出日時 | 2022-03-04 23:13:56 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 127 ms / 2,500 ms |
コード長 | 1,677 bytes |
コンパイル時間 | 1,968 ms |
コンパイル使用メモリ | 178,640 KB |
実行使用メモリ | 121,728 KB |
最終ジャッジ日時 | 2024-07-18 23:16:59 |
合計ジャッジ時間 | 9,544 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 MLE * 4 |
ソースコード
#include <bits/stdc++.h>using namespace std;#ifdef _RUTHEN#include "debug.hpp"#else#define show(...) true#endifusing ll = long long;#define rep(i, n) for (int i = 0; i < (n); i++)template <class T> using V = vector<T>;int main() {ios::sync_with_stdio(false);cin.tie(0);ll N, K;cin >> N >> K;V<ll> A(N);rep(i, N) cin >> A[i];V<V<int>> dp1(N + 1, V<int>(K + 1, 0)), dp2(N + 1, V<int>(K + 1, -1));dp1[0][0] = 1;rep(i, N) {for (int k = 0; k <= K; k++) {if (dp1[i][k] == 0) continue;dp1[i + 1][k] = 1;dp2[i + 1][k] = k;if (k + A[i] <= K) {dp1[i + 1][k + A[i]] = 1;dp2[i + 1][k + A[i]] = k;}}}// show(dp1, dp2);if (dp1[N][K] == 0) {cout << -1 << '\n';return 0;}set<int> S;int k0 = K;for (int i = N; i > 0; i--) {if (dp2[i][k0] != k0) {// addS.insert(i - 1);k0 = dp2[i][k0];}}V<V<int>> dp3(N + 1, V<int>(K + 1, 0));dp3[N][0] = 1;for (int i = N - 1; i >= 0; i--) {for (int k = 0; k <= K; k++) {if (dp3[i + 1][k] == 0) continue;dp3[i][k] = 1;if (k + A[i] <= K) {dp3[i][k + A[i]] = 1;}}}int ans = 0;show(S);for (auto &s : S) {show(s);int ok = 0;for (int k = 0; k <= K; k++) {if (dp1[s][k] && dp3[s + 1][K - k]) {ok = 1;}}if (ok == 0) ans++;}cout << ans << '\n';return 0;}