Zju2109 fatmouse 'trade-simple greedy

Source: Internet
Author: User

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

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.