結果

問題 No.247 線形計画問題もどき
ユーザー yuppe19 😺yuppe19 😺
提出日時 2016-07-06 11:21:40
言語 C++11
(gcc 11.4.0)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 479 bytes
コンパイル時間 310 ms
コンパイル使用メモリ 51,992 KB
最終ジャッジ日時 2024-04-27 02:21:25
合計ジャッジ時間 679 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:8:3: error: ‘vector’ was not declared in this scope
    8 |   vector<int> a(n, 0);
      |   ^~~~~~
main.cpp:3:1: note: ‘std::vector’ is defined in header ‘<vector>’; did you forget to ‘#include <vector>’?
    2 | #include <algorithm>
  +++ |+#include <vector>
    3 | using namespace std;
main.cpp:8:10: error: expected primary-expression before ‘int’
    8 |   vector<int> a(n, 0);
      |          ^~~
main.cpp:9:41: error: ‘a’ was not declared in this scope
    9 |   for(int i=0; i<n; ++i) { scanf("%d", &a[i]); }
      |                                         ^
main.cpp:10:10: error: expected primary-expression before ‘int’
   10 |   vector<int> dp(c+1, inf);
      |          ^~~
main.cpp:11:3: error: ‘dp’ was not declared in this scope
   11 |   dp[0] = 0;
      |   ^~
main.cpp:13:24: error: ‘a’ was not declared in this scope
   13 |     for(int acc=0; acc+a[i]<=c; ++acc) {
      |                        ^
main.cpp:7:18: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
    7 |   int c, n; scanf("%d%d", &c, &n);
      |             ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <algorithm>
using namespace std;

int main(void) {
  constexpr int inf = 987654321;
  int c, n; scanf("%d%d", &c, &n);
  vector<int> a(n, 0);
  for(int i=0; i<n; ++i) { scanf("%d", &a[i]); }
  vector<int> dp(c+1, inf);
  dp[0] = 0;
  for(int i=0; i<n; ++i) {
    for(int acc=0; acc+a[i]<=c; ++acc) {
      dp[acc+a[i]] = min(dp[acc+a[i]], dp[acc] + 1);
    }
  }
  int res = dp[c];
  if(res == inf) { res = -1; }
  printf("%d\n", res);
  return 0;
}
0