Description:
It is also fatmouse, which has m-pound cat food. The cat looks after the fatmouse's favorite food an warehouse, and the No. I warehouse contains J [I] pound JavaBean, f [I] pound food is required for exchange. Of course, you do not need to exchange all of them, but only pay for them in proportion. Ask the maximum number of an that fatmouse can exchange.
Analysis:
SimpleGreedy. Each time you go to the unit cat food in exchange for a warehouse with a maximum of JavaBean.
Set the repository accordingJ [I]/f [I] in descending orderAnd then exchange the JavaBean greedy.
Sort qsort in the C library.
I did not tell the number of warehouses about the question.500-1000. However, the strange thing is that I use the qsort sorting program to reduce N to 5, which can also be AC!
I changed the version to the sorting version, but I re ...... Depressed. If you don't believe it, try ......
Also, the data seems to beIf f [I] is not 0.
# Include <stdio. h>
# Include <stdlib. h>
# Define INF 1e10
# Define n 1001
Typedef struct {
Int J, F;
Double R;
} Node;
Node A [n];
Int CMP (const void * a, const void * B ){
If (node *) A)-> r> (node *) B)-> r) Return-1;
Else if (node *) a)-> r <(node *) B)-> r) return 1;
Else return 0;
}
Int main ()
{
Int I, j, k, m, n;
Double food, bean;
While (scanf ("% d", & m, & n), m! =-1)
{
For (I = 0; I <n; I ++ ){
Scanf ("% d", & a [I]. j, & a [I]. f );
// If (! A [I]. f) a [I]. r = INF; else
A [I]. r = (double) a [I]. j/a [I]. f;
}
Qsort (A, N, sizeof (A [0]), CMP );
Food = m;
Bean = 0;
For (I = 0; I <n; I ++ ){
If (A [I]. F <= food ){
Food-= A [I]. F;
Bean + = A [I]. J;
} Else {
Bean + = (Food/A [I]. f) * A [I]. J;
Break;
}
}
Printf ("%. 3lf/N", bean );
}
Return 0;
}