結果

問題 No.247 線形計画問題もどき
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-02-26 20:29:51
言語 C++11
(gcc 11.4.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,421 bytes
コンパイル時間 665 ms
コンパイル使用メモリ 74,744 KB
実行使用メモリ 46,800 KB
最終ジャッジ日時 2023-10-23 20:22:09
合計ジャッジ時間 4,861 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <complex>
#include <queue>
#include <map>
#include <string>
using namespace std;

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define FORR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

#define EPS (1e-10)
#define EQ(a,b) (abs((a)-(b)) < EPS)

#define PI 3.1415926535

typedef long long ll;
typedef pair<int, int> P;
//typedef complex<double> C;

const int INF = 99999999;
const int MAX_N = 100;
const int MAX_C = 100000;

int C, N;
int a[MAX_N];
// dp[i][j] = i番目までの和がjとなるようなsum(x)(i=0..i)の最小値(存在しなければ-1)
int dp[MAX_N][MAX_C + 1];

void input() {
	cin >> C >> N;
	REP(i, N) cin >> a[i];
}

void solve() {
	REP(i, N) REP(j, C + 1) dp[i][j] = INF;

	// x0について
	int t_sum = 0;
	int x0 = 0;
	while (t_sum <= C) {
		dp[0][t_sum] = x0;
		t_sum += a[0];
		x0++;
	}

	// 配るDP
	REP(i, N - 1) {
		REP(j, C + 1) {
			if (dp[i][j] != INF) {
				int nxt_sum = j;
				int nxt_x = 0;
				while (nxt_sum <= C) {
					dp[i + 1][nxt_sum] = min(dp[i + 1][nxt_sum], dp[i][j] + nxt_x);
					nxt_sum += a[i + 1];
					nxt_x++;
				}
			}
		}
	}

	if (dp[N - 1][C] != INF) {
		cout << dp[N - 1][C] << endl;
	} else {
		cout << -1 << endl;
	}
}

int main() {
	input();
	solve();
}
0