結果

問題 No.527 ナップサック容量問題
ユーザー bal4ubal4u
提出日時 2019-09-05 19:20:03
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 1,156 bytes
コンパイル時間 543 ms
コンパイル使用メモリ 29,492 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-09-02 22:01:35
合計ジャッジ時間 2,692 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,372 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 1 ms
4,368 KB
testcase_03 AC 1 ms
4,368 KB
testcase_04 AC 1 ms
4,368 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 6 ms
4,372 KB
testcase_07 AC 4 ms
4,372 KB
testcase_08 AC 2 ms
4,372 KB
testcase_09 AC 1 ms
4,372 KB
testcase_10 AC 5 ms
4,372 KB
testcase_11 AC 3 ms
4,368 KB
testcase_12 AC 3 ms
4,372 KB
testcase_13 AC 7 ms
4,372 KB
testcase_14 AC 2 ms
4,368 KB
testcase_15 AC 3 ms
4,372 KB
testcase_16 AC 1 ms
4,368 KB
testcase_17 AC 3 ms
4,372 KB
testcase_18 AC 3 ms
4,372 KB
testcase_19 AC 1 ms
4,372 KB
testcase_20 AC 2 ms
4,368 KB
testcase_21 AC 7 ms
4,372 KB
testcase_22 AC 4 ms
4,372 KB
testcase_23 AC 7 ms
4,368 KB
testcase_24 AC 1 ms
4,372 KB
testcase_25 AC 4 ms
4,368 KB
testcase_26 AC 4 ms
4,372 KB
testcase_27 AC 4 ms
4,368 KB
testcase_28 AC 3 ms
4,368 KB
testcase_29 AC 7 ms
4,368 KB
testcase_30 AC 1 ms
4,368 KB
testcase_31 AC 1 ms
4,372 KB
testcase_32 AC 1 ms
4,372 KB
testcase_33 AC 1 ms
4,368 KB
testcase_34 AC 1 ms
4,372 KB
testcase_35 AC 3 ms
4,368 KB
testcase_36 AC 1 ms
4,372 KB
testcase_37 AC 2 ms
4,372 KB
testcase_38 AC 3 ms
4,372 KB
testcase_39 AC 3 ms
4,368 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: 関数 ‘in’ 内:
main.c:9:14: 警告: 関数 ‘getchar_unlocked’ の暗黙的な宣言です [-Wimplicit-function-declaration]
    9 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:15:24: 備考: in expansion of macro ‘gc’
   15 |         int n = 0, c = gc();
      |                        ^~

ソースコード

diff #

// yukicoder: 527 ナップサック容量問題
// 2019.9.5 bal4u

#include <stdio.h>
#include <string.h>

//// 高速入力
#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

int in() {   // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

int N;
int dp[100005];
int v[103], w[103], V;

// a[i]>=key という条件を満たす最小のi
int lower_bound(int *a, int sz, int x) {
  int m, l = 0, r = sz;
  while (l+1 < r) {
    m = (l+r)/2;
    if (a[m] < x) l = m; else r = m;
  }
  return a[l] < x? r: l;
}

inline static void chmax(int *r, int a, int b) {
	if (a < b) a = b;
	if (*r < a) *r = a;
}	

int main()
{
	int i, j, b;
	
	N = in(); for (i = 1; i <= N; i++) v[i] = in(), w[i] = in();
	V = in();
	memset(dp, -1, sizeof(dp));
	dp[0] = 0, b = 0;
	for (i = 1; i <= N; i++) {
		b += w[i];
		for (j = b; j >= w[i]; j--) if (dp[j-w[i]] >= 0) {
			chmax(&dp[j], dp[j-w[i]] + v[i], dp[j]);
		}
	}
	for (i = 0; dp[i] < V; i++);
	if (i == 0) i = 1;
	printf("%d\n", i);
	if (i == b) puts("inf");
	else {
		while (dp[i] <= V) i++;
		printf("%d\n", i-1);
	}
	return 0;
}
0