Submission #54471
ソースコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | #include "bits/stdc++.h" // Begin {{{ using namespace std; #define all(x) x.begin(), x.end() #define rep(i, n) for (i64 i = 0; i < (n); ++i) #define reps(i, b, e) for (i64 i = (b); i <= (e); ++i) #define repr(i, b, e) for (i64 i = (b); i >= (e); --i) #define var(Type, ...) Type __VA_ARGS__; input(__VA_ARGS__) #ifdef DEBUG #define dump(...) dump_(#__VA_ARGS__, __VA_ARGS__); #else #define dump(...) #endif using i64 = int_fast64_t; using pii = pair<i64, i64>; template < class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true ); } template < class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true ); } constexpr int INF = 0x3f3f3f3f; constexpr i64 LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr int MOD = int (1e9) + 7; // input void input() {} template < class Head, class ... Tail> void input(Head&& head, Tail&&... tail) { cin >> head; input(forward<Tail>(tail)...); } // outs void outs() {} template < class Head, class ... Tail> void outs(Head&& head, Tail&&... tail) { cout << head << " \n" [ sizeof ...(tail)==0]; outs(forward<Tail>(tail)...); } template < class T> void outs(vector<T> &vec) { for (auto &e : vec) cout << e << " \n" [&e==&vec.back()]; } template < class T> void outs(vector<vector<T>> &df) { for (auto &vec : df) OUTS(vec); } // outl void outl() {} template < class Head, class ... Tail> void outl(Head&& head, Tail&&... tail) { cout << head << "\n" ; outl(forward<Tail>(tail)...); } template < class T> void outl(vector<T> &vec) { for (auto &e : vec) cout << e << "\n" ; } // outn void outn() {} template < class Head, class ... Tail> void outn(Head&& head, Tail&&... tail) { cout << head; outn(forward<Tail>(tail)...); } template < class T> void outn(vector<T> &vec) { for (auto &e : vec) cout << e; } // dump template < class T> void dump_( const char *s, T&& x) { clog << '{' ; while (*s != '\0' ) clog << *(s++); clog << ": " << x << '}' << "\n" ; } template < class Head, class ... Tail> void dump_( const char *s, Head&& head, Tail&&... tail) { clog << '{' ; while (*s != ',' ) clog << *(s++); clog << ": " << head << "}, " ; for (++s; ! isgraph (*s); ++s); dump_(s, forward<Tail>(tail)...); } // }}} End i64 N, W; vector<i64> v(110), w(110); vector<vector<i64>> dp(110, vector<i64>(11000, -1)); i64 solve(i64 i, i64 j) { if (i >= N) return 0; if (dp[i][j] != -1) return dp[i][j]; if (j + w[i] > W) return dp[i][j] = solve(i + 1, j); return dp[i][j] = max({solve(i + 1, j + w[i]) + v[i], solve(i, j + w[i]) + v[i], solve(i + 1, j)}); } signed main() { ios::sync_with_stdio( false ); cin.tie(nullptr); input(N, W); rep(i, N) { input(v[i], w[i]); } outl(solve(0, 0)); return 0; } |
ステータス
項目 | データ |
---|---|
問題 | 0239 - ナップザック問題(Medium) |
ユーザー名 | もけ |
投稿日時 | 2019-09-08 13:04:06 |
言語 | C++14 |
状態 | Accepted |
得点 | 3 |
ソースコード長 | 2830 Byte |
最大実行時間 | 52 ms |
最大メモリ使用量 | 10964 KB |
セット
セット | 得点 | Cases | |
---|---|---|---|
1 | ALL | 3 / 3 | * |
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|---|---|---|---|
DPL_1_C_in1.txt | AC | 47 ms | 10076 KB |
1
|
DPL_1_C_in2.txt | AC | 26 ms | 10120 KB |
1
|
DPL_1_C_in3.txt | AC | 29 ms | 10272 KB |
1
|
DPL_1_C_in4.txt | AC | 33 ms | 10240 KB |
1
|
DPL_1_C_in5.txt | AC | 29 ms | 10312 KB |
1
|
DPL_1_C_in6.txt | AC | 23 ms | 10340 KB |
1
|
DPL_1_C_in7.txt | AC | 27 ms | 10232 KB |
1
|
DPL_1_C_in8.txt | AC | 24 ms | 10388 KB |
1
|
DPL_1_C_in9.txt | AC | 27 ms | 10412 KB |
1
|
DPL_1_C_in10.txt | AC | 26 ms | 9972 KB |
1
|
DPL_1_C_in11.txt | AC | 23 ms | 10108 KB |
1
|
DPL_1_C_in12.txt | AC | 28 ms | 10104 KB |
1
|
DPL_1_C_in13.txt | AC | 25 ms | 9984 KB |
1
|
DPL_1_C_in14.txt | AC | 26 ms | 10336 KB |
1
|
DPL_1_C_in15.txt | AC | 23 ms | 10180 KB |
1
|
DPL_1_C_in16.txt | AC | 26 ms | 10640 KB |
1
|
DPL_1_C_in17.txt | AC | 27 ms | 10432 KB |
1
|
DPL_1_C_in18.txt | AC | 29 ms | 10204 KB |
1
|
DPL_1_C_in19.txt | AC | 29 ms | 10228 KB |
1
|
DPL_1_C_in20.txt | AC | 25 ms | 10140 KB |
1
|
DPL_1_C_in21.txt | AC | 24 ms | 10168 KB |
1
|
DPL_1_C_in22.txt | AC | 26 ms | 10324 KB |
1
|
DPL_1_C_in23.txt | AC | 28 ms | 10324 KB |
1
|
DPL_1_C_in24.txt | AC | 52 ms | 10280 KB |
1
|
DPL_1_C_in25.txt | AC | 31 ms | 10276 KB |
1
|
DPL_1_C_in26.txt | AC | 34 ms | 10404 KB |
1
|
DPL_1_C_in27.txt | AC | 28 ms | 10200 KB |
1
|
DPL_1_C_in28.txt | AC | 29 ms | 10224 KB |
1
|
DPL_1_C_in29.txt | AC | 26 ms | 10244 KB |
1
|
DPL_1_C_in30.txt | AC | 27 ms | 10288 KB |
1
|
DPL_1_C_in31.txt | AC | 30 ms | 10424 KB |
1
|
DPL_1_C_in32.txt | AC | 29 ms | 10164 KB |
1
|
DPL_1_C_in33.txt | AC | 38 ms | 10188 KB |
1
|
DPL_1_C_in34.txt | AC | 41 ms | 10200 KB |
1
|
DPL_1_C_in35.txt | AC | 26 ms | 10348 KB |
1
|
DPL_1_C_in36.txt | AC | 31 ms | 10372 KB |
1
|
DPL_1_C_in37.txt | AC | 40 ms | 10272 KB |
1
|
DPL_1_C_in38.txt | AC | 44 ms | 10296 KB |
1
|
DPL_1_C_in39.txt | AC | 35 ms | 10964 KB |
1
|
DPL_1_C_in40.txt | AC | 44 ms | 10904 KB |
1
|