Skip to main content
  1. Posts/

hdu 1203 I NEED A OFFER! (01背包)

·2 mins
Note: This article is available in Chinese only. 本文暂无英文版本。 View original

I NEED A OFFER!
#

**Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18287    Accepted Submission(s): 7320 **

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 ‘%’.

01背包

最开始WA在初始化,dp数组全都赋值为1,忘赋了dp[0],导致如果恰好是花光所有钱得到答案的时候答案错误。

然后还一直WA。

后来发现是当N=0的时候,我直接取了所有A[I]=0对应的B[I]中最大的值。。。这显然是错的,少年你是脑抽吗。。。。。

然后修改了下,终于A掉了。。。。。

 1
 2    /* ***********************************************
 3    Author :111qqz
 4    Created Time :2016年02月22日 星期一 22时50分40秒
 5    File Name :code/hdu/1203.cpp
 6    ************************************************ */
 7
 8    #include <iostream>
 9    #include <algorithm>
10    #include <cstring>
11    #include <cstdio>
12    #include <cmath>
13
14    using namespace std;
15        int n,m;
16        const int N = 1E4+5;
17        int a[N];
18        double b[N],dp[N];
19        double ans,mmax;
20
21    void solve(int cost,double value)
22    {
23        for ( int i = n ; i >= cost ; i-- )
24            dp[i] = min(dp[i],dp[i-cost]*value);
25    }
26
27    void init()
28    {
29        for ( int i = 0 ; i <N ; i++ )
30            dp[i]=1.0;
31        mmax=-1;
32        ans=1;
33       // memset(a,0,sizeof(a));
34       // memset(b,0,sizeof(b));
35    }
36    int main()
37    {
38        while (scanf("%d %d",&n,&m)!=EOF)
39        {
40            init();
41            if ( (n==0)&&(m==0) ) break;
42            for ( int i = 1 ; i <= m ; i++ )
43                scanf("%d %lf",&a[i],&b[i]);
44            for ( int i = 1 ; i <= m ; i++ )
45                b[i]=1-b[i];
46            if ( n==0 )
47            {
48                for ( int i = 1 ; i <= m ; i++ )
49                    if ( a[i]==0 )
50                        ans = ans*b[i];
51                printf("%.1lf%%\n",(1-ans)*100);
52                continue;
53            }
54
55            for ( int i = 1 ; i <= m ; i++ )
56                solve(a[i],b[i]);
57                ans = 1-dp[n];
58                ans = ans*100;
59          //  for ( int i = 1 ; i <= n ; i++ )
60            //    printf("%lf ",dp[i]);
61            printf("%.1lf%%\n",ans);
62        }
63        return 0;
64    }

Related

hdu 5119 - Happy Matt Friends(dp解法)

·2 mins
Description Matt has N friends. They are playing a game together. Each of Matt’s friends has a magic number. In the game, Matt selects some (could be zero) of his friends. If the xor (exclusive-or) sum of the selected friends’magic numbers is no less than M , Matt wins.

HDOJ 4882 Loves Codefires

·2 mins
ZCC Loves Codefires Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 988 Accepted Submission(s): 500 Problem Description Though ZCC has many Fans, ZCC himself is a crazy Fan of a coder, called “Memset137”. It was on Codefires(CF), an online competitive programming site, that ZCC knew Memset137, and immediately became his fan. But why? Because Memset137 can solve all problem in rounds, without unsuccessful submissions; his estimation of time to solve certain problem is so accurate, that he can surely get an Accepted the second he has predicted. He soon became IGM, the best title of Codefires. Besides, he is famous for his coding speed and the achievement in the field of Data Structures. After become IGM, Memset137 has a new goal: He wants his score in CF rounds to be as large as possible. What is score? In Codefires, every problem has 2 attributes, let’s call them Ki and Bi(Ki, Bi>0). if Memset137 solves the problem at Ti-th second, he gained Bi-KiTi score. It’s guaranteed Bi-KiTi is always positive during the round time. Now that Memset137 can solve every problem, in this problem, Bi is of no concern. Please write a program to calculate the minimal score he will lose.(that is, the sum of Ki*Ti).

poj 1065 Wooden Sticks

·2 mins
Wooden Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19008 Accepted: 8012 Description There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows: (a) The setup time for the first wooden stick is 1 minute. (b) Right after processing a stick of length l and weight w , the machine will need no setup time for a stick of length l’ and weight w’ if l <= l’ and w <= w’. Otherwise, it will need 1 minute for setup. You are to find the minimum setup time to process a given pile of n wooden sticks. For example, if you have five sticks whose pairs of length and weight are ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) , and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1 , 2 ) , ( 2 , 5 ) . Input

codeforces 447 B. DZY Loves Strings

·1 min
简单贪心。 因为填的字母没有次数限制,所以最优策略很容易想到,就是在最后面填最大的。 不用实际去填,算出ans就可以。