zoukankan      html  css  js  c++  java
  • hdu2955 Robberies  01背包+概率

    link:http://acm.hdu.edu.cn/showproblem.php?pid=2955

    首先,这个题目的背包容量不能是概率.1.精度不清楚.2.把概率相加有什么意义呢?所以,转换一下,把所有银行的珠宝和当作背包容量,把小偷安全的概率当作物品价值.可以先求出背包尽可能满的情况下,安全概率最大的解.然后在这些解里面,找出安全概率满足大于1-P的并且价值最大的就行.

    题目读清楚.人给的是被抓住的概率和每个银行被抓住的概率.这个是不能直接用的.比如连续偷几个银行,就要分别算出安全的概率,这样概率就可以直接相乘了.

    只要要偷的几个银行的安全概率的积大于1-P就行.

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cmath>
     6 #include <cctype>
     7 #include <algorithm>
     8 #include <queue>
     9 #include <deque>
    10 #include <queue>
    11 #include <list>
    12 #include <map>
    13 #include <set>
    14 #include <vector>
    15 #include <utility>
    16 #include <functional>
    17 #include <fstream>
    18 #include <iomanip>
    19 #include <sstream>
    20 #include <numeric>
    21 #include <cassert>
    22 #include <ctime>
    23 #include <iterator>
    24 const int INF = 0x3f3f3f3f;
    25 const int dir[8][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
    26 using namespace std;
    27 int V, n, c[11111];
    28 double w[11111], f[11111];
    29 int main(void)
    30 {
    31     ios::sync_with_stdio(false);
    32     #ifndef ONLINE_JUDGE
    33     freopen("in.txt", "r", stdin);
    34     #endif // ONLINE_JUDGE
    35     int t; cin>>t;
    36     while (t--)
    37     {
    38         double p;
    39         cin>>p>>n;
    40         p = 1-p;
    41         V = 0;
    42         for (int i = 0; i <n; ++i)
    43         {
    44             cin>>c[i]>>w[i]; V+=c[i];
    45             w[i]=1-w[i];
    46         }
    47         for (int i = 0; i <= V; ++i) f[i] = 0;
    48         f[0] = 1;
    49         for (int i = 0; i < n; ++i)
    50         {
    51             for (int v = V; v>=c[i]; --v)
    52             {
    53                 f[v] = max(f[v], f[v-c[i]]*w[i]);
    54             }
    55         }
    56         int ans=-1;
    57         for (int i = V; i>=0; --i)
    58         {
    59             if (f[i] >= p && i>ans) ans=i;
    60         }
    61         cout<<ans<<endl;
    62     }
    63 }

    这两天过的很颓废啊.

  • 相关阅读:
    IT程序猿”是怎样练成的? 之 提升内驱力的7大秘籍转
    wp7 退出程序的提示对话框
    生活不容易
    Decorator模式学习
    用序列化方法实现的Prototype的深拷贝
    Observer pettern
    Adapter模式学习
    bridge模式学习
    Composite模式学习
    Prototype原形设计模式
  • 原文地址:https://www.cnblogs.com/liuxueyang/p/3240134.html
Copyright © 2011-2022 走看看