結果
問題 |
No.1861 Required Number
|
ユーザー |
|
提出日時 | 2022-03-07 20:27:23 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 47 ms / 2,500 ms |
コード長 | 1,555 bytes |
コンパイル時間 | 1,774 ms |
コンパイル使用メモリ | 169,228 KB |
実行使用メモリ | 22,988 KB |
最終ジャッジ日時 | 2024-07-22 15:49:02 |
合計ジャッジ時間 | 3,973 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 RE * 4 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i, f, n) for (int i = (f); i < (n); i++) #define rrep(i, f, n) for (int i = int(n) - 1; i >= f; i--) typedef long long ll; typedef pair<int, int> P; template <typename T, typename U> auto Sum(T a, U b) -> decltype(a + b) { return a + b; } template <typename T, typename U> inline bool amax(T &a, U b) { if (b > a) { a = b; return true; } return false; } template <typename T, typename U> inline bool amin(T &a, U b) { if (b < a) { a = b; return true; } return false; } int main() { int N, K; cin >> N >> K; vector<int> A(N + 1); rep(i, 1, N + 1) cin >> A[i]; bool dpl[N + 2][K + 1], dpr[N + 2][K + 1]; rep(i, 0, N + 2) rep(j, 0, K + 2) { dpl[i][j] = false; dpr[i][j] = false; } dpl[0][0] = true; dpr[N + 1][0] = true; rep(i, 1, N + 1) rep(j, 0, K + 1) { dpl[i][j] = dpl[i - 1][j]; if (j >= A[i]) dpl[i][j] |= dpl[i - 1][j - A[i]]; } rrep(i, 1, N + 1) rep(j, 0, K + 1) { dpr[i][j] = dpr[i + 1][j]; if (j >= A[i]) dpr[i][j] |= dpr[i + 1][j - A[i]]; } if (!dpl[N][K]) { cout << -1 << endl; return 0; } int ans = 0; rep(i, 1, N + 1) { bool ok = true; rep(j, 0, K + 1) { if (dpl[i - 1][j] && dpr[i + 1][K - j]) ok = false; } if (ok) ans++; } cout << ans << endl; }