Problem Description
E_star和von是中国赫赫有名的两位商人,俗话说的好无商不奸,最近E_star需要进一批苹果。可是他需要的苹果只有von才有,von的苹果都存在他的传说中很牛叉的仓库里,每个仓库都存了不同种类的苹果,而且每个仓库里的苹果的价钱不同。如果E_star想要买仓库i里的所有重量为f[i]的苹果他必须付m[i]的金钱。E_star开着他的传说中的毛驴车去拉苹果,而且他只带了N些金钱。E_star作为传说中的奸商希望用它所带的N金钱得到重量最多的苹果。你作为他最好的朋友,所以他向你求出帮助。希望你能帮忙计算出他能买到最多的苹果(这里指重量最大)。并输出最大重量。
提示:这里仅考虑仓库里苹果的重量,不考虑个数。
Input
第一行包括两个非负整数N,M(分别代表E_star带的金币数,von盛苹果的仓库数量,不超过50)。
接下来有有M行,每行包括两个数非负整数f[i]和m[i]分别表示第i仓库里存有重量为f[i]的苹果,如果将所有苹果买下要花费m[i]的金钱,E_star不必非要将每个仓库的苹果全部买下。
当M,N二者中任一为-1时结束。
Output
E_star用N的金币所能买到的最大重量的苹果的重量。结果保留三位小数。
Sample Input
5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1
Sample Output
13.333 31.500
Hint
Source
E_st
错误的:
01 |
#include <stdio.h> |
02 |
struct node |
03 |
{ |
04 |
int E , von; |
05 |
double jia; |
06 |
}shop[10001]; |
07 |
void pai( struct node shop [] , int l , int r) |
08 |
{ |
09 |
int i = l , j = r; |
10 |
struct node k = shop[l]; |
11 |
if (l >= r) |
12 |
{ |
13 |
return ; |
14 |
} |
15 |
while (i < j) |
16 |
{ |
17 |
while (i < j && shop[j].jia <= k.jia) |
18 |
{ |
19 |
j--; |
20 |
} |
21 |
shop[i] = shop[j]; |
22 |
while (i < j && shop[i].jia >= k.jia) |
23 |
{ |
24 |
i++; |
25 |
} |
26 |
shop[j] = shop[i]; |
27 |
} |
28 |
shop[i] = k; |
29 |
pai(shop , i + 1 , r); |
30 |
pai(shop , l , i - 1); |
31 |
} |
32 |
int main () |
33 |
{ |
34 |
int n , m , i; |
35 |
while (~ scanf ( "%d%d" , &n ,&m) &&(n != -1 || m!= -1)) |
36 |
{ |
37 |
for (i = 0 ; i < m ; i++) |
38 |
{ |
39 |
scanf ( "%d%d" , &shop[i].E , &shop[i].von); |
40 |
shop[i].jia = shop[i].E *(1.000 / shop[i].von); |
41 |
} |
42 |
pai(shop , 0 , m - 1); |
43 |
i= 0; |
44 |
double sum = shop[i].E; |
45 |
int z = shop[0].von; |
46 |
i = 1; |
47 |
while (z < n) |
48 |
{ |
49 |
if (z + shop[i].von <= n) |
50 |
{ |
51 |
sum += shop[i].E; |
52 |
z += shop[i].von; |
53 |
} |
54 |
else |
55 |
{ |
56 |
sum += (n - z) * shop[i].jia ; |
57 |
break ; |
58 |
} |
59 |
i++; |
60 |
} |
61 |
printf ( "%.3lf\n" , sum); |
62 |
} |
63 |
return 0; |
64 |
} |
65 |
66 |
正确的:
01 |
#include <stdio.h> |
02 |
struct node |
03 |
{ |
04 |
int E , von; |
05 |
double jia; |
06 |
}shop[10001]; |
07 |
void pai( struct node shop [] , int l , int r) |
08 |
{ |
09 |
int i = l , j = r; |
10 |
struct node k = shop[l]; |
11 |
if (l >= r) |
12 |
{ |
13 |
return ; |
14 |
} |
15 |
while (i < j) |
16 |
{ |
17 |
while (i < j && shop[j].jia <= k.jia) |
18 |
{ |
19 |
j--; |
20 |
} |
21 |
shop[i] = shop[j]; |
22 |
while (i < j && shop[i].jia >= k.jia) |
23 |
{ |
24 |
i++; |
25 |
} |
26 |
shop[j] = shop[i]; |
27 |
} |
28 |
shop[i] = k; |
29 |
pai(shop , i + 1 , r); |
30 |
pai(shop , l , i - 1); |
31 |
} |
32 |
int main () |
33 |
{ |
34 |
int n , m , i; |
35 |
while (~ scanf ( "%d%d" , &n ,&m) &&(n != -1 || m!= -1)) |
36 |
{ |
37 |
for (i = 0 ; i < m ; i++) |
38 |
{ |
39 |
scanf ( "%d%d" , &shop[i].E , &shop[i].von); |
40 |
shop[i].jia = shop[i].E *(1.000 / shop[i].von); |
41 |
} |
42 |
pai(shop , 0 , m - 1); |
43 |
|
44 |
double sum = 0; |
45 |
for (i = 0; i < m; i++) |
46 |
{ |
47 |
if (n > shop[i].von) |
48 |
{ |
49 |
sum += shop[i].E; |
50 |
n -= shop[i].von; |
51 |
} |
52 |
53 |
54 |
else |
55 |
{ |
56 |
sum += 1.0 * n* shop[i].jia; |
57 |
break ; |
58 |
} |
59 |
} |
60 |
61 |
printf ( "%.3lf\n" , sum); |
62 |
} |
63 |
return 0; |
64 |
} |
01 |
#include <stdio.h> |
02 |
struct node |
03 |
{ |
04 |
int E , von; |
05 |
double jia; |
06 |
}shop[10001]; |
07 |
void pai( struct node shop [] , int l , int r) |
08 |
{ |
09 |
int i = l , j = r; |
10 |
struct node k = shop[l]; |
11 |
if (l >= r) |
12 |
{ |
13 |
return ; |
14 |
} |
15 |
while (i < j) |
16 |
{ |
17 |
while (i < j && shop[j].jia <= k.jia) |
18 |
{ |
19 |
j--; |
20 |
} |
21 |
shop[i] = shop[j]; |
22 |
while (i < j && shop[i].jia >= k.jia) |
23 |
{ |
24 |
i++; |
25 |
} |
26 |
shop[j] = shop[i]; |
27 |
} |
28 |
shop[i] = k; |
29 |
pai(shop , i + 1 , r); |
30 |
pai(shop , l , i - 1); |
31 |
} |
32 |
int main () |
33 |
{ |
34 |
int n , m , i; |
35 |
while (~ scanf ( "%d%d" , &n ,&m) &&(n != -1 || m!= -1)) |
36 |
{ |
37 |
for (i = 0 ; i < m ; i++) |
38 |
{ |
39 |
scanf ( "%d%d" , &shop[i].E , &shop[i].von); |
40 |
shop[i].jia = shop[i].E *(1.000 / shop[i].von); |
41 |
} |
42 |
pai(shop , 0 , m - 1); |
43 |
|
44 |
double sum = 0; |
45 |
for (i = 0; i < m; i++) |
46 |
{ |
47 |
if (n > shop[i].von) |
48 |
{ |
49 |
sum += shop[i].E; |
50 |
n -= shop[i].von; |
51 |
} |
52 |
53 |
54 |
else |
55 |
{ |
56 |
sum += 1.0 * n* shop[i].jia; |
57 |
break ; |
58 |
} |
59 |
} |
60 |
61 |
printf ( "%.3lf\n" , sum); |
62 |
} |
63 |
return 0; |
64 |
} |