hdu1203 I NEED A OFFER!(01背包)
2016-04-06 21:51
344 查看
这题是求概率,所以dp求的是不被录取的概率,记住以后但凡出来概率题都要反向思考。剩余的钱越少,不被录取的概率越小,录取率越大,所以最后求的就是dp
。
话说每次做这题脑子里都要翻一个大弯,真是醉了。
注意初始化的时候不能用memset初始化,只能把n逐个初始化,我也不知道为啥。。。反正编译不出来。
还有一点,题中输出的是两数同为0时才跳出,因为这破条件我还WA了4次T T
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
const int N = 10010;
const int INF = 1e8;
int main()
{
// freopen("in.txt", "r", stdin);
int n, m;
double dp
, b
;
int a
;
while(~scanf("%d%d", &n, &m) && (n != 0) || (m != 0))
{
for(int i = 1; i <= m; i ++)
{
scanf("%d%lf", &a[i], &b[i]);
}
for(int i = 0; i <= n; i ++)
dp[i] = 1;
for(int i = 1; i <= m; i ++)
{
for(int j = n; j >= a[i]; j --)
dp[j] = min(dp[j], dp[j - a[i]] * (1 - b[i])); //不被录取的概率
}
printf("%.1lf%%\n", (1 - dp
) * 100);
}
return 0;
}
。
话说每次做这题脑子里都要翻一个大弯,真是醉了。
注意初始化的时候不能用memset初始化,只能把n逐个初始化,我也不知道为啥。。。反正编译不出来。
还有一点,题中输出的是两数同为0时才跳出,因为这破条件我还WA了4次T T
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
const int N = 10010;
const int INF = 1e8;
int main()
{
// freopen("in.txt", "r", stdin);
int n, m;
double dp
, b
;
int a
;
while(~scanf("%d%d", &n, &m) && (n != 0) || (m != 0))
{
for(int i = 1; i <= m; i ++)
{
scanf("%d%lf", &a[i], &b[i]);
}
for(int i = 0; i <= n; i ++)
dp[i] = 1;
for(int i = 1; i <= m; i ++)
{
for(int j = n; j >= a[i]; j --)
dp[j] = min(dp[j], dp[j - a[i]] * (1 - b[i])); //不被录取的概率
}
printf("%.1lf%%\n", (1 - dp
) * 100);
}
return 0;
}
相关文章推荐
- 【HDU 5366】The mook jong 详解
- 【HDU 2136】Largest prime factor 详细图解
- 【HDU 1568】Fibonacci 数学公式 详解
- HDU 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- 2015-11-11 hdu新生赛 A题(AC)
- 2015-11-11 hdu新生赛 C题(结束后一发AC)