I NEED A OFFER!

本文探讨了一种特殊的01背包问题,其中目标是最小化得不到offer的概率。通过使用动态规划解决,提供了两种实现方式:一种导致内存溢出,另一种通过一维数组成功解决问题。代码示例展示了如何进行状态转移。

这道题其实质是一个01背包问题,dp[i][j]表示选择i个学校时,花费j,不能得到一份officer的概率,其状态转移方程为dp[i][j]=min(dp[i-1][j],dp[i-1][j-cost[i]]*(1-p[i]).)

刚开始时我用的是二维数组,结果MLE,然后改为一维数组,AC了......

两种情况代码如下:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cost[1010];
float dp[10010];
float p[1010];
const int inf=0x7fffffff;
float fun(float x,float y)
{
  return x>y?y:x;
}
int main( )
{
  int N,M;
  while(scanf("%d%d",&N,&M)!=EOF&&(N||M))
  {
    int i,j,k,m,n;
    float sum=1;
    for(i=1;i<=M;i++)
    scanf("%d%f",&cost[i],&p[i]);
   // for(i=0;i<=M;i++)
     for(i=0;i<=N;i++)
      dp[i]=1;
   // for(i=0;i<=M;i++)
     //dp[i][0]=1;
      for(i=1;i<=M;i++) 
       for(j=N;j>=cost[i];j--)
       {
      dp[j]=fun(dp[j],dp[j-cost[i]]*(1-p[i]));
      //printf("%d:%.2f ",j,dp[j]);
      }
     printf("%.1f%%\n",(1-dp[N])*100);
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int cost[1010];
float dp[1010][10010];
float p[1010];
const int inf=0x7fffffff;
float fun(float x,float y)
{
  return x>y?y:x;
}
int main( )
{
  int N,M;
  while(scanf("%d%d",&N,&M)!=EOF&&(N||M))
  {
    int i,j,k,m,n;
    float sum=1;
    for(i=1;i<=M;i++)
    scanf("%d%f",&cost[i],&p[i]);
    for(i=0;i<=M;i++)
     for(j=0;j<=N;j++)
      dp[i][j]=1;
    for(i=0;i<=M;i++)
     dp[i][0]=1;
      for(i=1;i<=M;i++) 
       for(j=1;j<=N;j++)
        if(j>cost[i])
        {
         dp[i][j]=fun(dp[i-1][j],dp[i-1][j-cost[i]]*(1-p[i]));
        // printf("***i=%d: %.2f ",i,dp[i][j]);
         //puts(" ");
        }
        else
        {
        dp[i][j]=dp[i-1][j];
       // printf("i=%d : %.2f ",i,dp[i][j]);
       // puts(" ");
        }
     printf("%.1f%%\n",(1-dp[M][N])*100);
}
return 0;
}

转载于:https://www.cnblogs.com/tangcong/archive/2011/04/23/2025976.html

Dear Friend ; Especially for you - this amazing announcement . This is a one time mailing there is no need to request removal if you won't want any more ! This mail is being sent in compliance with Senate bill 2316 , Title 1 ; Section 303 ! This is not a get rich scheme . Why work for somebody else when you can become rich in 77 months . Have you ever noticed society seems to be moving faster and faster and more people than ever are surfing the web ! Well, now is your chance to capitalize on this ! We will help you turn your business into an E-BUSINESS and sell more . You can begin at absolutely no cost to you . But don't believe us . Ms Ames who resides in Indiana tried us and says "Now I'm rich, Rich, RICH" . We are licensed to operate in all states . If not for you then for your LOVED ONES - act now ! Sign up a friend and you'll get a discount of 30% ! Thank-you for your serious consideration of our offer . Dear Colleague ; Your email address has been submitted to us indicating your interest in our letter ! This is a one time mailing there is no need to request removal if you won't want any more ! This mail is being sent in compliance with Senate bill 2516 ; Title 1 , Section 304 . This is not a get rich scheme ! Why work for somebody else when you can become rich inside 34 months ! Have you ever noticed how long the line-ups are at bank machines plus people will do almost anything to avoid mailing their bills ! Well, now is your chance to capitalize on this ! We will help you process your orders within seconds and deliver goods right to the customer's doorstep ! You are guaranteed to succeed because we take all the risk ! But don't believe us . Mr Simpson of Oregon tried us and says "I've been poor and I've been rich - rich is better" . We are licensed to operate in all states . We urge you to contact us today for your own future financial well-being . Sign up a friend and you get half off . Thank-you for your serious consideration of our offer . Dear Salaryman ; Your email address has been submitted to us indicating your interest in our briefing . This is a one time mailing there is no need to request removal if you won't want any more ! This mail is being sent in compliance with Senate bill 1625 , Title 8 , Section 301 ! This is different than anything else you've seen ! Why work for somebody else when you can become rich in 38 weeks . Have you ever noticed how many people you know are on the Internet and nearly every commercial on television has a .com on in it ! Well, now is your chance to capitalize on this ! WE will help YOU deliver goods right to the customer's doorstep plus increase customer response by 160% ! You can begin at absolutely no cost to you . But don't believe us ! Prof Simpson who resides in Idaho tried us and says "I was skeptical but it worked for me" ! We are licensed to operate in all states ! We IMPLORE you - act now . Sign up a friend and you'll get a discount of 30% . Thanks . 找出flag
05-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值