ZOJ 2109 fatmouse& #39; Trade (Backpack DP + greedy)

Source: Internet
Author: User

Links: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109

Fatmouse prepared M pounds of cat food, ready-to-trade with the cats guarding the warehouse containing he favorite food, JavaBean.
The warehouse has N rooms. The i-th contains j[i] pounds of JavaBeans and requires f[i] pounds of cat food. Fatmouse does not has the to trade for all the JavaBeans in the the the same, instead, he may get j[i]* a% of pounds JavaBeans if he Pays f[i]* a% pounds of cat food. Here A is a real number. Now he's assigning this homework to you:tell him the maximum amount of JavaBeans he can obtain.

Input
The input consists of multiple test cases. Each test case is begins with a line containing the non-negative integers M and N. Then N lines follow, each contains, non-negative integers j[i] and f[i] respectively. The last test case was followed by Two-1 ' s. All integers is not greater than 1000.

Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of J Avabeans that Fatmouse can obtain.

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


Translation:

There used to be only fat rats. His name is Fatmouse, he is like a human terrorist trading arms with the enemy, wretched he prepared a M-pound cat food, ready to trade with the big cats guarding the warehouse, the warehouse has his favorite food JavaBean.


There are n rooms in the warehouse, and in room j[i] pounds javabean and need to f[i] pound cat food exchange, fatmouse do not have to be in every room JavaBean is practical for trading, on the contrary. He can pay the big cat f[i]*a% pounds of cat food, thus changing the j[i]*a% pound of JavaBean. A is a real number, and now he gives you a homework assignment, please tell him how many pounds javabean he can get.
Input descriptive narrative:
The input includes multiple sets of test data, the first line of each test data is two non-negative integer m, N. In the next n rows, each row consists of two nonnegative integers j[i] and f[i], and the last set of test data is two-1. The value of all integers does not paste more than 1000.
Output Descriptive narrative:
For each set of test data, a real number of 3 decimal places is printed on one line, which is the maximum amount of javabean that fatmouse can trade.


Problem Solving Ideas:
The maximum volume of output is required. and retains three decimal places. In this way, we use j[i] divided by f[i] to get a, then, when trading, in order to obtain the most javabean, we must first trade a large. This ensures that the JavaBean can be traded to the maximum.
The data is read into the structure, then the structure as the elements of the vector, and then a from a large to a small order to the vector, and then the calculation in turn. This topic belongs to the Knapsack class topic!

(DP + greedy)


Code:

#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector > #include <set> #define MAXN 10005#define RST (n) memset (n, 0, sizeof (n)) #include <algorithm>using    namespace Std;typedef struct Mouse_ {double J, F; Double A;} Mouse;int N, m;vector <Mouse> v;vector <Mouse>:: Iterator It;bool CMP (const Mouse M1, const Mouse m2) {if (M    1.a! = m2.a) return m1.a > m2.a; ELSE return M1. F < m2. F;}        int main () {while (~SCANF ("%d%d", &n, &m)) {if (n = =-1 && m = =-1) break;        Mouse Mouse;        V.clear (); for (int i=0; i<m; i++) {scanf ("%lf%lf", &mouse. J, &mouse.            F); Mouse.a = mouse. J/mouse.            F        V.push_back (mouse);        } sort (V.begin (), V.end (), CMP);        Double sum = 0; for (int i=0; i<v.size (); i++) {if (n > V[i]. F) {sum + = V[i].                J N-= V[i].            F }else{sum + = N*V[I].A;            Break    }} printf ("%.3lf\n", sum); } return 0;}


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

ZOJ 2109 fatmouse& #39; Trade (Backpack DP + greedy)

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.