LightOJ 1079 Just another Robbery (01背包)

Posted dwtfukgv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;
}

  

以上是关于LightOJ 1079 Just another Robbery (01背包)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 2955 Robberies && LightOJ 1079 Just another Robbery

HDU 2955 Robberies && LightOJ 1079 Just another Robbery

lightoj 1079 Just another Robbery 概率 背包

1079 - Just another Robbery

Just another Robbery(概率&01背包)

LightOJ - 1079 概率dp