1. 程式人生 > >HDU 1203 【01背包/小數/概率DP】

HDU 1203 【01背包/小數/概率DP】

申請 stack 人的 cep eps Go cto BE limit

I NEED A OFFER!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 33247 Accepted Submission(s): 13449

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%

Hint

You should use printf("%%") to print a ‘%‘.

Author
Speakless

Source
Gardon-DYGG Contest 2

【分析】:
可以拿到至少一份offer的最大概率p1。這句話需要轉化為不拿到offer的最小概率p2 (dp[i]表示花費i萬元得不到offer的最小概率 )。p1 = 1 - p2 ,這樣就可以避免分類的情況。

第二:最大背包因為狀態轉移變成了最小背包,註意變化。(PS:1-w[i]是不拿到offer, min是最小概率

狀態轉移方程:dp[j]=min(dp[j],dp[j-w[i]]*(1.0–v[i]));

第三:輸入的最後有兩個0。按題意是m!=0&&n!=0 ,而實際上n||m才能過QAQ

【代碼】:

#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<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#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 fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> Pr;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int MAXN =  1e3 + 5;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
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};
const int maxn = 1e5+5;
const int mod=1e9+7;
int  w[maxn];
double dp[maxn],p[maxn];
int n,m;
int main()//dp[i]表示花費i萬元得不到offer的最小概率

{
    while(cin >> m >> n, n||m)
    {
        for(int i=0;i<=m;i++) dp[i]=1;//初始化為1.0
        for(int i=1;i<=n;i++){
            cin >> w[i] >> p[i];
            p[i] = 1.0-p[i];
        }
        for(int i=1; i<=n; i++){
            for(int j=m; j>=w[i]; j--){
               dp[j]=min(dp[j], dp[j-w[i]]*p[i]);
            }
        }
        printf("%.1f%%\n",(1.0-dp[m])*100);
    }
}
/*
10 3
4 0.1
4 0.2
5 0.3
0 0
*/

HDU 1203 【01背包/小數/概率DP】