01背包 基础

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.

InputThe 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 .OutputFor 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
  • 题意:其实这道题还是很坑的,就是有个人去抢银行了,每个银行可以抢的钱是Mj,抢钱被抓住的概率是Pj,给你一个最大被抓概率,问这时最多抢多少钱。
  • 思路:这道题的关键在于一般情况下,我们用背包容量来表示dp的数位,这个题目中背包大小变成了浮点数,就只能反过来存,dp[获得钱数]=概率;最后从大到小输出第一个概率大于等于Pj的dp的标点

代码:

import java.util.Arrays;
import java.util.Scanner;

public class E {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    while(t-->0){
        double v = sc.nextDouble();
        int n = sc.nextInt();
        double vo[] = new double[1050];
        int val[] = new int[1050];
        int sum = 0;
        for(int i=0;i<n;i++){
            val[i] = sc.nextInt();
            vo[i] = sc.nextDouble();
            sum+=val[i];
        }
        double[] dp = new double[10500];
        Arrays.fill(dp, 0);
        dp [0] = 1;
        for(int i=0;i<n;i++){
            for(int j=sum;j>=val[i];j--){
                dp[j] = Math.max(dp[j], dp[j-val[i]]*(1-vo[i]));
//                System.out.println(j+"  :"+dp[j]);
            }
        }
        for(int i=sum;i>=0;i--){
            if(dp[i]>=(1-v)){
                System.out.println(i);
                break;
            }
        }

    }
}
}
时间: 2024-11-05 11:24:07

01背包 基础的相关文章

01背包基础 (杭电2602)

01背包问题: 有一个体积为V的背包,有n件物品,每件物品的体积,价值分别为w[i],p[i];要从n件物品中选些放入背包中,使背包里物品的总价值最大. 动态方程:c[i][j]=max(c[i-1][j],c[i-1][j-w[i]]+p[i]). 有关动态方程方面的代码: for (int i = 1; i <= n; i++) { for (int j = 1; j <= total_weight; j++) { if (w[i] > j) { c[i][j] = c[i-1][j

Jam&#39;s balance HDU - 5616 (01背包基础题)

Jim has a balance and N weights. (1≤N≤20) The balance can only tell whether things on different side are the same weight. Weights can be put on left side or right side arbitrarily. Please tell whether the balance can measure an object of weight M. In

POJ - 3624 Charm Bracelet (01背包基础)

题意: 即是给你一个容量M的包,有N件物品,每件物品有分别对应的 价值value 以及 重量weight .然后在不超过该背包容量的情况下能得到的最大价值为多少? 思路: 由于这是最基础的问题,所以就记录当对 01背包状态转移方程式的 理解. 对于动态规划来说,首先要知道我们要确定哪些状态量.然后再基于这些状态量进行状态转移得到我们最后希望得到的答案. 比如对于序列求最值来说我们习惯记录最后位取的是谁(即末位j),那么同理我们也可以记录我们当前选的是哪一种 物品.同时容量在状态中是必不可少的,所

UVA 562 Dividing coins (01背包基础)

[题目链接]:click here~~ 代码: /* * Problem: UVA No.562 * Running time: 0MS * Complier: C++ * Author: ACM_herongwei * Create Time: 11:12 2015/9/9 星期三 * zeroonebags * 将金币总价值的一半作为背包容量,然后zeronebags */ #include <stdio.h> #include <iostream> #include <

01 背包基础 - 空间优化 (滚动数组,一维阵列)

2017-09-03 11:39:16 writer:pprp 以很简单的一个动态规划问题为引入: 从左上角到右下角走过的路径和最大,问你最大为多少? 1.可以想到普通的dp 状态转移为: dp[i][j] = max(dp[i-1][j],dp[i][j-1]) + arr[i][j]; 2.采用滚动数组的方式-节约了不必要的空间 状态转移为:dp2[i%2][j] = max(dp2[(i+1)%2][j],dp2[i%2][j-1]) + arr[i][j]; 3.采用一维阵列的方式更加节

hdu 2602 Bone Collector (01 背包基础)

http://acm.hdu.edu.cn/showproblem.php?pid=2602 题意 : n个骨头 m的容量 给出n个骨头的 value 和 volume 求m能容纳的最大价值 思路 : dp[j]=max(dp[j],dp[j-w[i]]+v[i]); #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main() { int n,m,t; in

01背包+卡精度 Hdu 2955

<span style="color:#3333ff;">/* ----------------------------------------------------------------------------- author : Grant Yuan time : 2014.7.19 aldorithm: 01背包+卡精度 ------------------------------------------------------------------------

南阳oj 860又见01背包

又见01背包 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述     有n个重量和价值分别为wi 和 vi 的 物品,从这些物品中选择总重量不超过 W 的物品,求所有挑选方案中物品价值总和的最大值. 1 <= n <=100 1 <= wi <= 10^7 1 <= vi <= 100 1 <= W <= 10^9 输入 多组测试数据. 每组测试数据第一行输入,n 和 W ,接下来有n行,每行输入两个数,代表第i个物品的wi 和

HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4739    Accepted Submission(s): 2470 Problem Description The title of this problem is familiar,isn't it?yeah,if you had took pa