結果
問題 | No.1861 Required Number |
ユーザー |
|
提出日時 | 2022-03-04 22:28:21 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 133 ms / 2,500 ms |
コード長 | 1,881 bytes |
コンパイル時間 | 1,788 ms |
コンパイル使用メモリ | 174,848 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-18 23:05:54 |
合計ジャッジ時間 | 8,591 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 MLE * 4 |
ソースコード
#include <bits/stdc++.h>using namespace std;using ll = long long;using VI = vector<int>;using P = pair<int, int>;#define REP(i, n) for (int i = 0; i < (int)(n); i++)#define FOR(i, a, b) for (ll i = a; i < (ll)(b); i++)#define ALL(a) (a).begin(),(a).end()constexpr int INF = 1001001001;constexpr ll LINF = 1001001001001001001ll;constexpr int DX[] = {1, 0, -1, 0};constexpr int DY[] = {0, 1, 0, -1};template< typename T1, typename T2>inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true); }template< typename T1, typename T2>inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true); }const ll MOD = 1000000007;int main() {int N, K;cin >> N >> K;VI A(N);REP(i, N) cin >> A[i];vector<vector<bool>> L(N + 1, vector<bool>(K + 1, false));vector<vector<bool>> R(N + 1, vector<bool>(K + 1, false));L[0][0] = true;R[N][0] = true;REP(i, N) {REP(j, K + 1) {L[i + 1][j] = L[i + 1][j] | L[i][j];if (j + A[i] <= K) L[i + 1][j + A[i]] = L[i + 1][j + A[i]] | L[i][j];R[N - i - 1][j] = R[N - i - 1][j] | R[N - i][j];if (j + A[N - 1 - i] <= K) R[N - i - 1][j + A[N - 1 - i]] = R[N - i - 1][j + A[N - 1 - i]] | R[N - i][j];}}if (!L[N][K]) {cout << -1 << endl;return 0;}int ans = 0;REP(i, N) {bool f = true, g = false;REP(j, K + 1) {if (L[i][j]) {if (R[i + 1][K - j]) f = false;if (j + A[i] <= K && R[i + 1][K - j - A[i]]) g = true;}}if (f && g) {// cout << i << endl;ans++;}}cout << ans << endl;/*REP(i, N + 1) {REP(j, K + 1) cout << L[i][j];cout << endl;REP(j, K + 1) cout << R[i][j];cout << endl << endl;}*/}