From the perspective of this question, we can see that it is a combination of a full backpack and multiple backpacks;
For shopping, there are multiple backpacks, and the store is a full backpack;
The initialization problem of the backpack is used here. Because the minimum value is obtained, the initial value should be as large as possible, which is greater than all possible values;
It is worth mentioning that the maximum number of backpacks is 20000, not the read T;
Use two arrays to record the minimum number of currencies required separately. One Dimension is enough;
Multiple monotonous queues are hard to understand .. only binary-optimized multiple backpacks are used;
The following code is provided:
# Include <iostream> # include <algorithm> # define maxn 100010 # define min (A, B) (A> B? B: A) int V [maxn], C [maxn], F [maxn], G [maxn], n, T; using namespace STD; int main () {int COUNT = 0; // freopen ("3591.txt"," r ", stdin); While (~ Scanf ("% d", & N, & T), N, T) {int I, j, k; for (I = 0; I <N; I ++) scanf ("% d", V + I); for (I = 0; I <n; I ++) scanf ("% d ", c + I); for (I = 1; I <maxn; I ++) f [I] = G [I] = maxn; f [0] = G [0] = 0; for (I = 0; I <n; I ++) {If (V [I] * C [I]> = 20000) // complete backpack {for (j = V [I]; j <= 20000; j ++) f [J] = min (F [J], F [J-V [I] + 1); continue;} k = 1; while (k <C [I]) // 0-1 backpack {for (j = 20000; j> = K * V [I]; j --) f [J] = min (F [J], F [J-K * V [I] + k); C [I]-= K; K * = 2;} For (j = 20000; j> = C [I] * V [I]; j --) f [J] = min (F [J], f [J-C [I] * V [I] + C [I]) ;}for (I = 0; I <n; I ++) for (j = V [I]; j <= 20000; j ++) g [J] = min (G [J], G [J-V [I] + 1); int ans = maxn; for (I = T; I <= 20000; I ++) {If (ANS> F [I] + G [I-t]) ans = f [I] + G [I-t];} If (ANS = maxn) ans =-1; printf ("case % d: % d \ n", ++ count, ANS);} return 0 ;}