Problem-solving ideas: First ignore the rice card balance is equal to 5 yuan to buy rice this detail, need to ask is the meal card inside the remaining money, conversion, become the most money, then the rest of the money is the least, then consider the balance of more than 5 dollars to buy rice this detail, you can think, if the balance of the Cary less than 5, , directly output the amount of the card now, if the card is more than 5 dollars, we can first save the 5 pieces of money, so as to ensure that it every time to buy a meal card balance is greater than 5, and finally the remaining 5 dollars to buy the most expensive dishes, so that the price of the dish to be sorted. After this conversion, it can be transformed into a 01 pack problem with a capacity of m-5 to get the maximum value.
Reflection: The first thing to do is to write the equation F[v]=max (f[v],f[v-c[i]]+c[i]), but it's always wrong. Or it should be converted into a 01-pack model.
Problem description UESTC part of the canteen meal card has a very strange design, that is, before purchasing to determine the balance. If the remaining amount on the card is greater than or equal to 5 yuan before the purchase of an item, the purchase must be successful (even if the balance is negative on the card after purchase), it cannot be purchased (even if the amount is sufficient). So we all want to try to keep the balance on the card to the minimum. One day, the canteen has n vegetables for sale, each vegetable can be purchased once. If you know the price of each vegetable and the balance on the card, ask at least how much of the balance on the card. Input multiple sets of data. For each set of data: The first action positive integer n, which represents the number of dishes. n<=1000. The second line consists of n positive integers representing the price per vegetable. The price does not exceed 50. The third line includes a positive integer m, which represents the balance on the card. m<=1000.
N=0 indicates the end of the data. Output for each set of inputs, outputs a line that contains an integer that represents the smallest possible balance on the card. Sample INPUT1505101 2 3 2 1 1 2 3 2 Output-4532 sample
#include <stdio.h> #include <string.h>int a[1010],f[1010];int max (int a,int b) { if (a>b) return A; else return b;} void Bubblesort (int a[],int n) { int i,j,t; for (i=1;i<=n;i++) {for (j=i+1;j<=n;j++) { if (A[i]>a[j]) { t=a[i]; A[I]=A[J]; a[j]=t;}}}} int main () { int n,i,j,v,m,t; while (scanf ("%d", &n)!=eof&&n) { memset (f,0,sizeof (f)); for (i=1;i<=n;i++) scanf ("%d", &a[i]); scanf ("%d", &m); Bubblesort (a,n); if (m>=5) {for (i=1;i<n;i++) {for (v=m-5;v>=a[i];v--) F[v]=max (F[v],f[v-a[i]] +a[i]); } printf ("%d\n", M-f[m-5]-a[n]); } else printf ("%d\n", m); }}
Hangzhou Electric 2546 Rice card "01 Backpack"