It seems that they have all done this question. I have read it several times and have not understood the meaning of the question (English is a hard injury ....) Today, I am chatting with cainiao and think about this question.
Water is greedy. Sort each group of data by price. Select cheaper and then expensive.
[Cpp] # include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Define N 1005
Struct node
{
Int x, y;
Double pri;
} A [N];
Int cmp (const void * a, const void * B)
{
Return (* (node *) a). pri> (* (node *) B). pri? 1:-1;
}
Int main ()
{
Int n, m;
While (scanf ("% d", & n, & m), m! =-1 & n! =-1)
{
Int I;
For (I = 0; I <m; I ++)
{
Scanf ("% d", & a [I]. x, & a [I]. y );
If (a [I]. x = 0)
{
A [I]. pri = 10000000;
Continue;
}
A [I]. pri = 1.0 * a [I]. y/a [I]. x;
}
Qsort (a, m, sizeof (a [0]), cmp );
Double ans;
Ans = 0;
For (I = 0; I <m; I ++)
{
If (n> a [I]. y)
{
Ans + = a [I]. x;
N-= a [I]. y;
}
Else if (n = a [I]. y)
{
Ans + = a [I]. x;
Break;
}
Else
{
Ans + = 1.0 * n * a [I]. x/a [I]. y;
Break;
}
}
Printf ("%. 3f \ n", ans );
}
Return 0;
}