Hdu3591 the trouble of Xiaoqian

Source: Internet
Author: User

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 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.