Description: How can Roy grab the most money without being caught. It provides the money that each bank can grab and the probability of being caught.
Analysis: at the beginning, we thought that the probability was only two decimal places, multiplied by 100, and we found that the data was not like this. We decided to wa. Then I am stuck here. What should I do if the size of my backpack is not an integer. In desperation, I looked at other people's problem-solving reports and found that the money I grabbed can be seen as the size of my backpack. the probability of being caught is the value of my backpack. (I am dying to learn it myself ). But I am wrong again. I want to add the probability that the bank is not caught every time. Anyone who knows the probability knows that this is a silly idea (o ).
The following code is used:
/* Hdu 2955 Robberies DP 0-1 backpack transfer equation: f [j] = max {f [j], f [j-v] * p} f [j] indicates the maximum escape probability when the money is snatched. */# include <iostream> using namespace std; const int maxn = 10005; double p [105], f [maxn], P; int val [105], tot; int N; double Max (double a, double B) {return a> B? A: B;} int main () {int t; cin> t; while (t --) {cin> P> N; tot = 0; for (int I = 0; I <N; I ++) {cin> val [I]> p [I]; tot + = val [I];} for (int I = 1; I <= tot; I ++) f [I] = 0; f [0] = 1; for (int I = 0; I <N; I ++) {for (int j = tot; j> = val [I]; j --) {f [j] = Max (f [j], f [j-val [I] * (1-p [I]);} // for (int k = 1; k <= tot; k ++) cout <f [k] <""; // cout <endl ;}for (int I = tot; I> = 0; I --) {if (f [I] >=( 1-P) {// cout <f [I] <endl; cout <I <endl; break; }}return 0 ;}
I didn't quite understand how to use a one-dimensional array. Why is it recursive in reverse order? I thought about it for half a day and came up with a clue.
Each loop uses a one-dimensional array to store information, so that the original information is overwritten each time. Therefore, you must update the vertex information related to the vertex before the vertex information is overwritten. In this way, only reverse loops are allowed. This question is here.
However, there are still some gains:
1) Select the size of the backpack and the value of the items when you are doing the 0-1 backpack. Just like this question.
(2) The value of an item can not only be added but also multiplied. Just like this question.
(3) the initialization of the transfer array should also be noted. Different questions have different initialization methods.
Okay, that's it. :-D