POJ 1276 Cash Machine

Source: Internet
Author: User

POJ 1276 Cash Machine water problem

Description: Given the number of items N and the target value K, each item has a CI, the value of VI, the last can be obtained not more than the maximum value of K. 、

Naked can no longer be naked backpack problem.

Multiple backpacks can also be converted to full backpack qaq directly.

Well.. This time why the transfer is not judged F[J] whether the existence (that is to determine the value of J can be reached).

Because I don't have to, because I'm stupid, qaq.

Because our state defines the maximum value that can be reached when the target is J, unlike the last question that counts the number of items used. The last question needs to be judged because. Each State must be transferred by the number of coins in the previous state, and the last value must be reached before it can be transferred.

And this time the F-array is only a maximum value. It's not up to the same. Value or 0 + current val. Have no effect on the results.

Instead. If we add such a verdict.

May cause some of the value to be reached in the F-array is not updated.

Sad Bloody lesson to Qaq. No matter what you write, you have to go through your head. Just like the strong LJQ (ORZLJQ) said not to first see what you will be directly to the bait, but first think about how this problem should be written in a better way.

Slag Code:

1#include <iostream>2#include <string.h>3#include <math.h>4#include <algorithm>5#include <stdlib.h>6#include <stdio.h>7 #definell Long Long8 using namespacestd;9 Const intN =100100;Ten intF[n], num[n]; One structqwq{intVal, num; } a[ -]; A intN, Targ; - //bool Yes[n]; - intMain () the { -      while(~SCANF ("%d%d", &targ, &N)) { -Memset (F,0,sizeof(f)); -         //memset (yes, 0, sizeof (yes)); +          for(inti =1; I <= N; i + +){ -             intAA, BB; +scanf"%d%d", &aa, &BB); A             if(!BB | |!aa) {i--, n--;Continue; } atA[i] =(qwq{BB, aa}); -         } -         if(!targ | |!n) {Puts ("0");Continue; }  -         //yes[0] = 1; -          for(inti =1; I <= N; i + +){ -memset (NUM,0,sizeof(num)); in              for(intj = A[i].val; J <= Targ; J + +){ -                 if(F[j-a[i].val] + a[i].val > F[j] && num[j-a[i].val] +1<=a[i].num) { toF[J] = F[j-a[i].val] +A[i].val; +NUM[J] = Num[j-a[i].val] +1; -                     //Yes[j] = 1; the                 } *             } $         }Panax Notoginsengprintf"%d\n", F[targ]); -     } the     return 0; +}
Full Backpack
1#include <iostream>2#include <string.h>3#include <math.h>4#include <algorithm>5#include <stdlib.h>6#include <stdio.h>7 #definell Long Long8 using namespacestd;9 Const intN =100100;Ten intF[n], num[n]; One structqwq{intVal, num; } a[ -]; A intN, Targ; - voidZeroOne (intVal) { for(inti = Targ; I >= Val; I--)if(F[i-val] + val > f[i]) f[i] = F[i-val] +Val;} - voidCompleteintVal) { for(inti = Val; I <= Targ; i + +)if(F[i-val] + val > f[i]) f[i] = F[i-val] +Val;} the voidMultiintValintnum) - { -     if(Num * val >=Targ) { -Complete (val);return ; +     } -     intTMP =1; +      while(Num >tmp) { AZeroOne (TMP *val); atnum-=tmp; -TMP <<=1; -     } -     if(num) zeroone (num *val); - } - intMain () in { -      while(~SCANF ("%d%d", &targ, &N)) { toMemset (F,0,sizeof(f)); +          for(inti =1; I <= N; i + +){ -             intAA, BB; thescanf"%d%d", &aa, &BB); *             if(!BB | |!aa) {i--, n--;Continue; } $A[i] =(qwq{BB, aa});Panax Notoginseng         } -         if(!targ | |!n) {Puts ("0");Continue; }  the          for(inti =1; I <= N; i + +){ + multi (A[i].val, a[i].num); A         } theprintf"%d\n", F[targ]); +     } -     return 0; $}
Multiple Backpack

Nailed IT.

--------------------------

Ah, recently my right eye vision has dropped sharply. The left eye is also strangely farsighted sad. Do not want to wear glasses qaq later in addition to study or less staring at computers and mobile phone q! A! Q (that should have been the case)

Words heard Nest Meng to change the head of the felling happy. I do not know the new teacher is swollen like. I don't know if I'll bother you if I go to the computer room Qaq

POJ 1276 Cash Machine

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.