hdu--2955 robberies (0-1 backpack)

Source: Internet
Author: User

Test instructions: There are n banks, each robbing a bank, can get (v_i\) before, but there will be (p_i\) probability of being caught. Now you have to control the probability of being caught under \ (p\) , to find out how much money can be robbed.
Analysis: 0-1 The deformation of the backpack, the weight into a probability, because the calculation of probability requires the product rather than the addition, so can not be directly used Dp[j] to express the probability of J maximum profit.
Make \ (dp[i][j]\) indicates to the former \ (i\) Bank, the probability that the value is (j\) can still remain secure, then there is a recursive type:
\[DP[I][J] = dp[i-1][j-v[i]]* (1-p[i]) \]
The first dimension can actually save, because it is related to the previous item, then push backwards like a 0-1 backpack.
Finally, we can find the maximum benefit that satisfies the safety probability.

  #include <bits/stdc++.h>using namespace std; #define EPS 1e-7const int maxn = 1e4+5;typedef Long Long ll;d        Ouble dp[maxn],p[maxn];int V[maxn];int Main () {#ifndef Online_judge freopen ("In.txt", "R", stdin);    Freopen ("OUT.txt", "w", stdout); #endif int T;    scanf ("%d", &t);    Dp[0] = 1.0;        while (t--) {int n,sum=0;        Double P;        cin>>p>>n;        P = 1-p;        Memset (Dp,0,sizeof (DP));        Dp[0] = 1;            for (int i=1;i<=n;++i) {cin>>v[i]>>p[i];            P[i] = 1-p[i];        Sum + = V[i];                     } for (int i=1;i<=n;++i) {for (int j=sum;j>=v[i];--j) {if (Dp[j-v[i]]*p[i] > Dp[j])            DP[J] = dp[j-v[i]]* P[i];                }} for (int i=sum;i>=0;--i) {if (dp[i]>p) {cout<<i<<endl;            Break }}}} return 0;}  

hdu--2955 robberies (0-1 backpack)

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.