結果

問題 No.247 線形計画問題もどき
ユーザー motimoti
提出日時 2015-07-21 20:48:44
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,103 bytes
コンパイル時間 683 ms
コンパイル使用メモリ 94,888 KB
実行使用メモリ 10,160 KB
最終ジャッジ日時 2023-09-22 20:58:58
合計ジャッジ時間 4,609 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
10,160 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 6 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,500 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 WA -
testcase_16 AC 2 ms
4,380 KB
testcase_17 WA -
testcase_18 TLE -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <set>
#include <map>
#include <limits>
#include <unordered_map>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;
typedef unsigned long long ull;

int N, C;
int A[101];

int constexpr invalid = std::numeric_limits<int>::max() / 6;

std::unordered_map<ull, ll> memo;

ll solve(int csum, int i, int xsum) {

  ull hash = (csum << 8) + (xsum << 8) + (i << 1);
  if(memo.find(hash) != memo.end()) { return memo[hash]; }

  if(i == -1) {
    if(csum == 0) { return xsum; }
    else { return invalid; }
  }

  if(csum < 0) { return invalid; }

  ll& ret = memo[hash];
  ret = invalid;

  for(int x = csum / A[i] + 1; x >= 0; x--) {
    ret = min(ret, solve(csum - A[i] * x, i-1, xsum + x));
  }

  return ret;
}

int main() {

  cin >> C >> N;
  rep(i, N) cin >> A[i];
  sort(A, A+N);
  auto ret = solve(C, N-1, 0);
  if(ret == invalid) {
    cout << -1 << endl;
  }
  else {
    cout << ret << endl;
  }

  return 0;
}
0