· Dynamic Programming Questions induction

backpack

1. fill a backpack type 01

   Reverse enumeration avoid after-effect 

 

Title Description

[Title] is intended to
    have a stick n (0≤n≤30), selected from a number of root length and s such that their closest V (a positive integer, 0≤v≤20000), and s <= v.
[Input format   
    an integer v, an integer n. Next, the n integer, which represent the length of the n stick.  
[Output Format]
    An integer representing vs.
[] Sample input
24
. 6
. 8. 7. 9. 3 12 is. 7
[Sample] outputs
0

 

#include<bits/stdc++.h>
using namespace std;
int a[350],v,n;
bool f[20000];
int main()
{
   cin>>v>>n;
   for(int i=1;i<=n;i++)cin>>a[i];
   f[0]=1;
   for(int i=1;i<=n;i++)
    for(int j=v;j>=a[i];j--)
     if(!f[j])f[j]=f[j-a[i]];
     
   int x=v;
   while(!f[x])x--;
   cout<<v-x;
   return 0;
}

 

Guess you like

Origin www.cnblogs.com/phemiku/p/11375202.html