zoukankan      html  css  js  c++  java
  • HDU-1203(01背包)

    I NEED A OFFER!

    Problem Description
    Speakless 很早就想出国,现在他已经考完了所有需要的考试,准备了所有要准备的材料,于是,便需要去申请学校了。要申请国外的任何大学,你都要交纳一定的申请费用, 这可是很惊人的。Speakless没有多少钱,总共只攒了n万美元。他将在m个学校中选择若干的(当然要在他的经济承受范围内)。每个学校都有不同的申 请费用a(万美元),并且Speakless估计了他得到这个学校offer的可能性b。不同学校之间是否得到offer不会互相影响。“I NEED A OFFER”,他大叫一声。帮帮这个可怜的人吧,帮助他计算一下,他可以收到至少一份offer的最大概率。(如果Speakless选择了多个学校,得 到任意一个学校的offer都可以)。
     
    Input
    输入有若干组数据,每组数据的第一行有两个正整数n,m(0<=n<=10000,0<=m<=10000)
    后面的m行,每行都有两个数据ai(整型),bi(实型)分别表示第i个学校的申请费用和可能拿到offer的概率。
    输入的最后有两个0。
     
    Output
    每组数据都对应一个输出,表示Speakless可能得到至少一份offer的最大概率。用百分数表示,精确到小数点后一位。
     
    Sample Input
    10 3 4 0.1 4 0.2 5 0.3 0 0
     
    Sample Output
    44.0%
     
    You should use printf("%%") to print a '%'.

    这道题目我想的很复杂啊,好吧,我错了。

    求得是的到offer的最大概率,反过来,就是求得不到offer的最小概率。然后再减
    dp[j] = min(dp[j], dp[j-val[i]*weg[i]]);
    算概率肯定是用之前得不到的概率 乘以 当前得不到的概率,然后作比。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 
     6 using namespace std;
     7 const int max_size = 10010;
     8 int val[max_size];
     9 double weg[max_size];
    10 bool vis[max_size];
    11 double dp[max_size];
    12 
    13 int main()
    14 {
    15     int vol, n;
    16 
    17     while(scanf("%d %d", &vol, &n) != EOF)
    18     {
    19         if(vol == 0 && n == 0)
    20             break;
    21         else
    22         {
    23             for(int i = 0; i < max_size; i++)
    24                 dp[i] = 1;
    25             for(int i = 0; i < n; i++)
    26             {
    27                 scanf("%d %lf", val+i, weg+i);
    28                 weg[i] = 1 - weg[i];
    29             }
    30 
    31             for(int i = 0; i < n; i++)
    32             {
    33                 for(int j = vol; j - val[i] >= 0; j--)
    34                 {
    35                     dp[j] = min(dp[j], dp[j-val[i]] * weg[i]);
    36                 }
    37             }
    38 
    39             printf("%0.1f%%
    ", 100 * (1 - dp[vol]));
    40         }
    41 
    42     }
    43     return 0;
    44 }
    View Code


  • 相关阅读:
    矩阵遍历 局部处理 661. 图片平滑器
    最小堆make_heap(), pop_heap()和push_heap()
    vector<pair<int, int>>或者有序map
    简单的滑动窗口 643. 子数组最大平均数 I
    js中getYear()和getFullYear()的区别
    异常:No WebApplicationContext found: no ContextLoaderListener registered?
    异常:Duplicate key or integrity constraint violation message from server: "Duplicate entry '0' for key 1"
    异常:the type net.sf.hibernate.lockmode cannot be resolved, it is indirectly referenced from required .class files
    使用cxf写web service的简单实例
    struts1多文件上传、下载实例
  • 原文地址:https://www.cnblogs.com/ya-cpp/p/4342781.html
Copyright © 2011-2022 走看看