結果

問題 No.393 2本の竹
ユーザー femto
提出日時 2016-07-12 01:40:12
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 562 ms / 1,000 ms
コード長 895 bytes
コンパイル時間 1,352 ms
コンパイル使用メモリ 69,980 KB
実行使用メモリ 241,792 KB
最終ジャッジ日時 2024-11-08 05:37:14
合計ジャッジ時間 15,980 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;

int a[60];
int s[60];
int dp[61][1000000];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int d;
	cin >> d;
	while(d--) {
		int n1, n2, m;
		cin >> n1 >> n2 >> m;
		if(n1 > n2) swap(n1, n2);

		for(int i = 0; i < m; i++) {
			cin >> a[i];
		}
		sort(a, a + m);
		for(int i = 1; i < m; i++) {
			s[i] = s[i - 1] + a[i - 1];
		}

		memset(dp, -1, sizeof dp);
		dp[0][0] = 0;
		for(int i = 0; i < m; i++) {
			for(int j = 0; j <= n1; j++) if(dp[i][j] != -1) {
				dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
				if(j + a[i] <= n1)
					dp[i + 1][j + a[i]] = max(dp[i + 1][j + a[i]], dp[i][j] + 1);
				if(s[i] - j + a[i] <= n2)
					dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + 1);
			}
		}
		cout << *max_element(dp[m], dp[m] + n1 + 1) << endl;
	}
}
0