Skip to main content
  1. Posts/

(dp专题004)hdu 2955Robberies(01背包变形)

·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

题目链接

题意: 给出n个银行 ,以及抢劫每个银行可以得到的价值和被抓的概率,不同银行之间被抓的概率是相互独立的,现在给出安全概率p,只有当概率从小于安全概率时才是安全的,问最多能抢劫多少价值。

思路:一开始很直接就想到把概率算成容量,

于是就成了经典的01背包,然后发现概率是double型。。。想当然得以为最多是2位小数,于是都*100转化成了整数做01背包。

然而正确思路是,把银行价值看成背包容量,而背包价值是概率!

将危险的概率转化成安全概率(1-危险概率=安全概率)

然后做01背包。

然后从大到小扫一遍价值,第一个大于安全概率的就是答案。

注意精度。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :Tue 15 Nov 2016 06:53:53 PM CST
 4File Name :code/hdu/2955.cpp
 5************************************************ */
 6#include <cstdio>
 7#include <cstring>
 8#include <iostream>
 9#include <algorithm>
10#include <vector>
11#include <queue>
12#include <stack>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <deque>
19#include <ctime>
20#define fst first
21#define sec second
22#define lson l,m,rt<<1
23#define rson m+1,r,rt<<1|1
24#define ms(a,x) memset(a,x,sizeof(a))
25typedef long long LL;
26#define pi pair < int ,int >
27#define MP make_pair
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33const int N=105;
34double dp[N];
35double p;
36int n,V;
37int value[N];
38double cost[N];
39void solve(double Cost,int Value)
40{
41        for ( int i = V ; i >= Value ; i--)
42                dp[i] = max(dp[i],dp[i-Value]*Cost);
43}
44int dblcmp( double d)
45{
46    return d<-eps?-1:d>eps;
47}
48int main()
49{
50  #ifndef  ONLINE_JUDGE
51    freopen("code/in.txt","r",stdin);
52  #endif
53   int T;
54   cin>>T;
55   while (T--)
56   {
57
58
59        scanf("%lf%d",&p,&n);
60        p = 1 - p;
61	V = 0 ;
62        for ( int i = 1; i <= n ; i++)
63        {
64                scanf("%d%lf",&value[i],&cost[i]);
65                cost[i] = 1 - cost[i];
66		V = V + value[i];
67        }
68	for ( int i = 0 ; i <= V ; i++) dp[i] =  0;
69        for ( int i = 1 ; i <= n ; i++)
70                solve(cost[i],value[i]);
71
72        for ( int i = V ; i >= 0 ; i--)
73	{
74	    if (dblcmp(dp[i]-p)>0)
75	    {
76		printf("%d\n",i);
77		break;
78	    }
79	}
80   }
81#ifndef ONLINE_JUDGE
82  fclose(stdin);
83  #endif
84    return 0;
85}

Related

BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster (dp,类似01背包)

·3 mins
# Time Limit: 5 Sec Memory Limit: 64 MB Submit: 504 Solved: 265 [Submit][Status][Discuss] Description # The cows are building a roller coaster! They want your help to design as fun a roller coaster as possible, while keeping to the budget. The roller coaster will be built on a long linear stretch of land of length L (1 <= L <= 1,000). The roller coaster comprises a collection of some of the N (1 <= N <= 10,000) different interchangable components. Each component i has a fixed length Wi (1 <= Wi <= L). Due to varying terrain, each component i can be only built starting at location Xi (0 <= Xi <= L-Wi). The cows want to string together various roller coaster components starting at 0 and ending at L so that the end of each component (except the last) is the start of the next component. Each component i has a “fun rating” Fi (1 <= Fi <= 1,000,000) and a cost Ci (1 <= Ci <= 1000). The total fun of the roller coster is the sum of the fun from each component used; the total cost is likewise the sum of the costs of each component used. The cows’ total budget is B (1 <= B <= 1000). Help the cows determine the most fun roller coaster that they can build with their budget.

hdu 1171 Big Event in HDU (母函数,01背包)

·3 mins
**Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26534 Accepted Submission(s): 9332 ** Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. But, maybe you don’t know that Computer College had ever been split into Computer College and Software College in 2002. The splitting is absolutely a big event in HDU! At the same time, it is a trouble thing too. All facilities must go halves. First, all facilities are assessed, and two facilities are thought to be same if they have the same value. It is assumed that there is N (0<N<1000) kinds of facilities (different value, different kinds).