hdu 2955 Robberies 01背包

本文通过解决一个关于银行抢劫的问题,介绍了如何利用01背包算法来确定最大可获得利益的同时确保被抓的概率低于一定阈值。文章详细解释了算法实现过程及核心代码。

Robberies

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 25589    Accepted Submission(s): 9432

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
 
Source

哇这个01背包真的是,以后好好学英语嗯 微笑
已经从网上看了无数个版的题解,终于。
嗯路还长慢慢走。

题意:想抢银行,但是有风险。
第一行是测试组数t,
第二行是抢银行需 要低于的概率(浮点型)p和计划抢的银行数n,
接下来n行,第一个数是银行钱数,第二个是被抢概率(浮点型)。
 题解:需要转换,把银行钱数j当成背包 价值v,被抓概率p当成重量p,求最大逃跑概率,然后输出最多的钱。
状态转移方程: dp[j] = max(dp[j],dp[j-bag[i].v]*(1-bag[i].p));  ( 概率是 (1-p1)* (1-p2)*(1-p3)...
#include<stdio.h> 
#include<string.h> 
#include<algorithm>  
using namespace std;  
const int maxn = 50005;
double dp[maxn]; 
struct zeroonebag  //定义01背包
{  
    int v;  
    double p;  
}bag[maxn];  
 
int main()  
{  
    int t,n;  
    double p;  
    scanf("%d",&t); 
    while(t--)  
    {  //需要低于的概率p,n是银行数, 
        scanf("%lf%d",&p,&n); 
        int sum = 0;  
        for(int i=0; i<n; i++)  
        { //bag[i].v 是银行钱数 bag[i].p 抢劫概率 
            scanf("%d%lf",&bag[i].v,&bag[i].p);  
            sum = sum+bag[i].v;  
        }  
        memset(dp,0,sizeof(dp));  
        dp[0] = 1;  //如果不抢就不会被抓 
 	//外层循环是银行数目,内层循环是银行钱数
        for(int i=0; i<n; i++)  
        {  
            for(int j=sum; j>=bag[i].v; j--)  
            {  
                dp[j] = max(dp[j],dp[j-bag[i].v]*(1-bag[i].p));  
            }  
        }  
        //抢钱的数目和逃跑的概率成反比,抢的越少,越容易跑 
        for(int i=sum; i>=0; i--)  
        {  
            if(dp[i] > 1-p)  //最大的逃跑概率大于被抓概率 ,这样才不会被抓
            {  
                printf("%d\n",i);  
                break;  
            }  
        }  
    }  
    return 0;  
}  

再附上一个把0.1背包拿出来的,enmmm反正也都一样..o(´^`)o

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 50005;
double dp[maxn];
int v[maxn];
double w[maxn];
int sum,n;

void zerobag(int cost,double weight)
{
	for(int i=sum; i>=cost; i--)
		dp[i] = max(dp[i],dp[i-cost]*(1-weight));
}

int main()
{
	int t;
	double p;
	scanf("%d",&t);
	while(t--){
		scanf("%lf%d",&p,&n);
		sum=0;
		for(int i=0; i<n; i++)
		{
			scanf("%d%lf",&v[i],&w[i]);
			sum += v[i];
		}
		memset(dp,0,sizeof(dp));
		dp[0]=1;
		for(int i=0; i<n; i++)
			zerobag(v[i],w[i]);
		for(int i=sum; i>=0; i--)
			if(dp[i]>=1-p)
			{
				printf("%d\n",i);
				break;
			}
	}
	return 0;
}

一花一天堂 一草一世界 一树一菩提  一土一如来 一方一净土 一笑一尘缘 一念一清净。

源码地址: https://pan.quark.cn/s/3916362e5d0a 在C#编程平台下,构建一个曲线编辑器是一项融合了图形用户界面(GUI)构建、数据管理及数学运算的应用开发任务。 接下来将系统性地介绍这个曲线编辑器开发过程中的核心知识点:1. **定制曲线面板展示数据曲线**: - 控件选用:在C#的Windows Forms或WPF框架中,有多种控件可用于曲线呈现,例如PictureBox或用户自定义的UserControl。 通过处理重绘事件,借助Graphics对象执行绘图动作,如运用DrawCurve方法。 - 数据图形化:通过线性或贝塞尔曲线连接数据点,以呈现数据演变态势。 这要求掌握直线与曲线的数学描述,例如两点间的直线公式、三次贝塞尔曲线等。 - 坐标系统与缩放比例:构建X轴和Y轴,设定坐标标记,并开发缩放功能,使用户可察看不同区间内的数据。 2. **在时间轴上配置多个关键帧数据**: - 时间轴构建:开发一个时间轴组件,显示时间单位刻度,并允许用户在特定时间点设置关键帧。 时间可表现为连续形式或离散形式,关键帧对应于时间轴上的标识。 - 关键帧维护:利用数据结构(例如List或Dictionary)保存关键帧,涵盖时间戳和关联值。 需考虑关键帧的添加、移除及调整位置功能。 3. **调整关键帧数据,通过插值方法获得曲线**: - 插值方法:依据关键帧信息,选用插值方法(如线性插值、样条插值,特别是Catmull-Rom样条)生成平滑曲线。 这涉及数学运算,确保曲线在关键帧之间无缝衔接。 - 即时反馈:在编辑关键帧时,即时刷新曲线显示,优化用户体验。 4. **曲线数据的输出**: - 文件类型:挑选适宜的文件格式存储数据,例如XML、JSON或...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值