Hdu 1009 (Water greedy)

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.