Link: http://acm.hdu.edu.cn/showproblem.php? PID = 2152 http://acm.hust.edu.cn/vjudge/contest/view.action? Cid = 28708 # Problem/efruit
Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2632 accepted submission (s): 1466
Problem description in the twinkling of an eye to the harvest season, due to the professional guidance of TT, Lele has gained a great harvest. In particular, there are N types of fruits in Lele, including apples, pears, bananas, and watermelons ...... It not only tastes delicious, but also looks nice.
As a result, many people come here to find Lele to buy fruit.
Even the well-known hdu acm Director lcy came. Lcy throws a dozen dollar notes. "I want to buy a fruit platter consisting of M fruits, but I have a small requirement that I have a limit on the number of each fruit, which cannot be less than a specific value, it cannot be greater than a specific value. And I don't want two identical disks. If you can match them at will, I will buy as many different solutions as you can! "
Now, please help Lele and help him calculate how many fruit disks can be sold to lcy.
Note that fruit is a basic unit and cannot be further divided. For the two schemes, if the numbers of fruits are the same, the two schemes are considered to be the same.
Lele eventually took the money and can continue his studies ~
Input this question contains multiple groups of tests. Please process it until the end of the file (EOF ).
The first line of each group of tests contains two positive integers n and M (for meanings, see the topic description, 0 <n, m <= 100)
Next, there are n rows of fruit information. Each row has two integers, A and B (0 <= A <= B <= 100), indicating that at least one fruit is required, you can only buy up to B fruits.
For each group of tests, output the total number of solutions that can be sold in one row.
Ensure that the answer is less than 10 ^ 9
Sample Input
2 31 21 23 50 30 30 3
Sample output
212
Authorlinle
Sourceacm Program Design final examination-(3
Tutorial 417)
Recommendlcy
Algorithm: grouping and knapsack thinking: stick to the principle that all functions belong to a backpack. About grouping and knapsack: Part 6: grouping and knapsack problems
P06: group backpack Problems
Problem
There are n items and a backpack with a capacity of v. The cost of the I-th item is C [I], and the value is W [I]. These items are divided into several groups. items in each group conflict with each other and you can select at most one item. Solving which items are loaded into a backpack can make the total cost of these items not exceed the capacity of the backpack, and the total value is the largest.
Algorithm
This problem becomes that there are several strategies for each group of items: whether to select one item in the group or not. That is to say, if f [k] [v] is set, it indicates the maximum weight value that can be obtained for the first K items in the Group. The options are:
F [k] [v] = max {f [k-1] [v], f [k-1] [V-C [I] + W [I] | item I belongs to group k}
The pseudocode for using a one-dimensional array is as follows:
For all group K
For v = V .. 0
For all I belong to group K
F [v] = max {f [v], F [V-C [I] + W [I]}
Pay attention to the order of the three-tier loop here, and I have written an error in the beta version of this article. The loop "for V = V .. 0" must be outside of "for all I belong to group K. In this way, only one item in each group can be added to the backpack.
In addition, it is obvious that "a simple and effective optimization" can be applied to items in each group in p02 ".
Summary
The grouping of backpacks refers to several items mutually exclusive to each other as a group, which establishes a good model. The deformation of many knapsack problems can be converted into a group's backpack problem (for example, p07). The concept of "Generalized items" can be further defined by the group's backpack problem, which is very helpful for solving the problem.
Homepage
From lecture 9 on backpacks
Then each fruit in the question exactly corresponds to the number of groups in the question,Items in the group conflict with each other. For each fruit type, you can only select a [I] to B [I] or none of them.Then there is a simple two-dimensional nesting.The strange thing is whether it is a one-dimensional error or a lack of profound understanding of the concept.
Later, I asked my younger brother nanchengbian, And he analyzed in depth the trap of rolling array optimization.The following conclusions are drawn:
The root cause of the optimization error is that the scrolling array is not completely overwritten!
The two-dimensional intervals (generally [0, backpack capacity]) for each cyclic solution of the highest dimensions (generally the item number) of most knapsack problems are the same.
If you use a rolling array to optimize it to one dimension, the current data will completely overwrite the previous data after each loop, and all the data called in the next loop will be updated.
However, the two-dimensional intervals ([A [I], M]) of each cyclic solution of the highest dimension (I) in this question are different!
If you use a rolling array to optimize it to one dimension, after each loop, the current data will only partially overwrite the previous data! This results in data residue. The data called during the next cycle may be residual data!
The correct method should be to use a rolling array to optimize it into two-dimensional
If you do not know much about a lot of things, you will be confused. This is very bad...Based on the principle that all functions are backpacks, we should systematically analyze all the types mentioned in section 9, not just the first three orz
Code: bare two-dimensional:
| 8909149 |
2013-08-11 21:25:28 |
Accepted |
2152 |
0 ms |
296 K |
790 B |
C ++ |
Freeze |
# Include <stdio. h> # include <string. h ># include <algorithm> using namespace STD; const int maxn = 110; int DP [maxn] [maxn]; int A [maxn]; int B [maxn]; int nvalue; int main () {int n, m; while (scanf ("% d", & N, & M )! = EOF) {memset (DP, 0, sizeof (DP); nvalue = m; DP [0] [0] = 1; for (INT I = 1; I <= N; I ++) scanf ("% d", & A [I], & B [I]); For (INT I = 1; I <= N; I ++) // n types correspond to N groups of items. The items in each group conflict with each other {for (Int J = m; j> = A [I]; j --) // size of the backpack {for (int K = A [I]; k <= B [I] & K <= J; k ++) DP [I] [J] + = DP [I-1] [J-K]; // The cost of the first I group items J }}printf ("% d \ n", DP [N] [m]);} return 0 ;}
After reading the summary, scroll through the optimized code of the array:
| 8911555 |
2013-08-12 09:57:26 |
Accepted |
2152 |
0 ms |
252 K |
844 B |
C ++ |
Freeze |
# Include <stdio. h> # include <string. h ># include <algorithm> using namespace STD; const int maxn = 110; int DP [2] [maxn]; int A [maxn]; int B [maxn]; int nvalue; int main () {int n, m; while (scanf ("% d", & N, & M )! = EOF) {memset (DP, 0, sizeof (DP); nvalue = m; DP [0] [0] = 1; for (INT I = 1; I <= N; I ++) scanf ("% d", & A [I], & B [I]); For (INT I = 1; I <= N; I ++) // n types correspond to N groups of items, and the items in each group conflict with each other {memset (DP [I % 2], 0, sizeof (DP [I % 2]); For (Int J = m; j> = A [I]; j --) // size of the corresponding backpack {for (int K = A [I]; k <= B [I] & K <= J; k ++) // All K belong to group I dp [I % 2] [J] + = DP [(I-1) % 2] [J-K];} printf ("% d \ n", DP [n % 2] [m]);} return 0 ;}