LightOJ 1079 Just another Robbery (01背包)

题意:给定一个人抢劫每个银行的被抓的概率和该银行的钱数,问你在他在不被抓的情况下,能抢劫的最多数量。

析:01背包,用钱数作背包容量,dp[j] = max(dp[j], dp[j-a[i] * (1.0 - pp[i])),dp[i] 表示不被抓的最大概率,在能抢劫到 i 个钱。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e4 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}

double dp[maxn];
int a[maxn];
double pp[maxn];

int main(){
  int T;  cin >> T;
  for(int kase = 1; kase <= T; ++kase){
    double p;
    scanf("%lf %d", &p, &n);
    p = 1.0 - p;
    memset(dp, 0, sizeof dp);
    m = 0;
    for(int i = 1; i <= n; ++i){
      scanf("%d %lf", a+i, pp+i);
      m += a[i];
    }
    dp[0] = 1.0;
    for(int i = 1; i <= n; ++i)
      for(int j = m; j >= a[i]; --j)
          dp[j] = max(dp[j], dp[j-a[i]] * (1.0 - pp[i]));

    int ans = 0;
    for(int i = m; i; --i)  if(dp[i] >= p){
      ans = i;  break;
    }
    printf("Case %d: %d\n", kase, ans);
  }
  return 0;
}

  

时间: 2024-08-28 23:29:32

LightOJ 1079 Just another Robbery (01背包)的相关文章

lightoj 1079 Just another Robbery 概率 背包

题目中给的都是被逮捕的概率p,并不方便计算,所以统统通过1-p,变成q,表示安全的概率. 然后这么多银行可以选择,有些类似背包问题.开始下意识认为应该是安全概率算体积,金钱算价值,更符合直观想法.但安全概率不是整数,这样子没法dp. 但是背包有个技巧,有时候体积可能是直观上的价值,我们不妨把金钱作为体积试试. dp[i][j]表示,考虑前i个银行,拿了j的金钱,的最大安全概率. dp[i][j] = max(dp[i - 1][j],dp[i - 1][j - v[i]] * q[i]),我们再

lightoj 1079 Just another Robbery 解题心得

原题: Description As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But his friends - H

[LightOJ 1079] Just another Robbery

Just another Robbery As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But his friend

LightOJ - 1079 Just another Robbery 概率 + dp

题目大意:harry要去抢劫银行,他手上有每个银行布局,所以他很清楚的了解到每个银行有多少钱,和去抢劫该银行被捕的概率 现在他要拟定一个计划,要求在被捕概率低于p的情况下,抢劫到最多的钱 解题思路:银行1个1个的抢过去,抢到的钱随着抢劫银行的数量增加或者不变(不变的情况就是不抢劫该银行),被捕的概率随着银行数量的增加而增大或者不变(不变的情况就是不抢劫该银行) 所以这个题有三个量在改变,一个是银行的数量,一个是抢到的钱,还有一个是被捕的概率 所以用二维dp来表示三个变量,设dp[i][j]表示抢

(概率 01背包) Just another Robbery -- LightOJ -- 1079

http://lightoj.com/volume_showproblem.php?problem=1079 Just another Robbery As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated r

LightOJ1079---Just another Robbery (概率做01背包)

As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (he wants everything quick!) so he decided to rob banks. He wants to make a calculated risk, and grab as much money as possible. But his friends - Hermione and Ron

hdu 2955 Robberies(01背包)

Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17723    Accepted Submission(s): 6544 Problem Description The aspiring Roy the Robber has seen a lot of American movies, and knows that

01背包+卡精度 Hdu 2955

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

HDU 2955 Robberies (01背包)

Robberies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11297    Accepted Submission(s): 4190 Problem Description The aspiring Roy the Robber has seen a lot of American movies, and knows that