HDU 1203 贪心或者DP

24 篇文章 0 订阅
9 篇文章 0 订阅

             贪心策略是申请offer需要的钱越少越好,而成功率越高越好,所以按照需要的钱和成功率的比值从小到大排序,依次选择。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 10010
using namespace std;
struct node
{
    int money;
    double p,r;
    bool operator<(const node &th) const
    {
        return th.r>r;
    }
};
node  chan[MAXN];
int main()
{
    //freopen("in.txt","r",stdin);
    int m,n;
    while(scanf("%d%d",&m,&n)!=EOF&&(m+n))
    {
        for(int i=0; i<n; i++)
        {
            scanf("%d%lf",&chan[i].money,&chan[i].p);
            chan[i].r=chan[i].money/chan[i].p;
        }
        sort(chan,chan+n);
        int sum=m;
        double rate=1;
        for(int i=0; i<n; i++)
        {
            if(sum>=chan[i].money)
            {
                sum-=chan[i].money;
                rate=rate*(1-chan[i].p);
            }
        }
        printf("%.1lf%%\n",(1-rate)*100);
    }
    return 0;
}

         下面是按照0-1背包思路做的,

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define MAXN 10010
using namespace std;
struct node
{
    int money;
    double p;
};
node  chan[MAXN];
int main()
{
    //freopen("in.txt","r",stdin);
    int m,n;
    while(scanf("%d%d",&m,&n)!=EOF&&(m+n))
    {
        for(int i=0; i<n; i++)
            scanf("%d%lf",&chan[i].money,&chan[i].p);
        double d[MAXN];
        for(int i=0;i<=m;i++)
            d[i]=1;
        for(int i=0; i<n; i++)
        {
            for(int j=m; j>=chan[i].money; j--)
            {
                if(d[j]>d[j-chan[i].money]*(1-chan[i].p))
                    d[j]=d[j-chan[i].money]*(1-chan[i].p);
            }
        }
     printf("%.1lf%%\n",(1-d[m])*100);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值