zoukankan      html  css  js  c++  java
  • hdu 2955 0-1背包问题

    http://acm.hdu.edu.cn/showproblem.php?pid=2955

    Problem Description
    The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually gets caught in the end, often because they become too greedy. He has decided to work in the lucrative business of bank robbery only for a short while, before retiring to a comfortable job at a university.


    For a few months now, Roy has been assessing the security of various banks and the amount of cash they hold. He wants to make a calculated risk, and grab as much money as possible.


    His mother, Ola, has decided upon a tolerable probability of getting caught. She feels that he is safe enough if the banks he robs together give a probability less than this.
     
    Input
    The first line of input gives T, the number of cases. For each scenario, the first line of input gives a floating point number P, the probability Roy needs to be below, and an integer N, the number of banks he has plans for. Then follow N lines, where line j gives an integer Mj and a floating point number Pj . 
    Bank j contains Mj millions, and the probability of getting caught from robbing it is Pj .
     
    Output
    For each test case, output a line with the maximum number of millions he can expect to get while the probability of getting caught is less than the limit set.

    Notes and Constraints
    0 < T <= 100
    0.0 <= P <= 1.0
    0 < N <= 100
    0 < Mj <= 100
    0.0 <= Pj <= 1.0
    A bank goes bankrupt if it is robbed, and you may assume that all probabilities are independent as the police have very low funds.
     
    Sample Input
    3
    0.04 3
    1 0.02
    2 0.03
    3 0.05
    0.06 3
    2 0.03
    2 0.03
    3 0.05
    0.10 3
    1 0.03
    2 0.02
    3 0.05
     
    Sample Output
    2
    4
    6

    问题涉及概率知识,当他去抢银行时被抓的概率是每个银行被抓的概率之积,因为只要一家抓到他他就落网了,是“或”的关系。然而,逃跑的概率是“与”的关系,必须全部逃走才算逃走,否则被抓。问题将所有银行的钱作为背包,以各家银行的钱作为花费,f[i]为偷到i的钱没被抓的概率,还有初值的问题,当i为0时,他没偷不被抓的概率为1所以有以下代码成立:

    #include <stdio.h>
    #include <string.h>
    double we[105],f[100005]={0};
    
    int main()
    {
        int n,t,i,j;
        double m,k;
        int cost[105];
        scanf("%d",&n);
        while(n--)
        {
            int sum=0;
            scanf("%lf%d",&m,&t);
            memset(f,0,sizeof(f));
            f[0]=1.0;
            for(i=0;i<t;i++)
            {
                scanf("%d%lf",&cost[i],&we[i]);
                sum+=cost[i];
            }
            for(i=0;i<t;i++)
            {
                for(j=sum;j>=cost[i];j--)
                {
                   f[j]=f[j]>(f[j-cost[i]]*(1-we[i]))?f[j]:(f[j-cost[i]]*(1-we[i]));
                }
            }
            for(i=sum;i>=0;i--)
            {
                if(f[i]>=(1-m))
                    {printf("%d
    ",i);break;}
                    //else printf("fbhweu");
            }
        }
        return 0;
    }
  • 相关阅读:
    如何用RadioButton做一个底部的切换栏
    自定义有监听器的ScrollView
    ViewPager的使用小技巧
    什么时候用Application的Context,什么时候用Activity的Context
    让改变输入法回车键的图标
    墙内下载DropBox离线安装包的方法
    巧用用layer-list做一个卡片背景
    LeakCanary中英文文档+使用例子
    让你的APK瘦成一道闪电
    用一张图片实现按钮按下和普通效果的样式
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3462495.html
Copyright © 2011-2022 走看看