Greedy questions
Calculate the value of JavaBean and sort it again
/* When ja is 0 or F is 0 */# include <iostream> # include <cstdio> # include <algorithm> # define INF 99999999 # define maxn 1005 using namespace STD; struct node {double Ja, F, price;} node [maxn]; bool CMP (node A, Node B) {return. price> B. price;} double greedy (int m, int N) {double sum = 0; For (INT I = 1; I <= N & M> = 0; I ++) {If (M = 0 & node [I]. f! = 0) break; If (M> = node [I]. f) {sum + = node [I]. ja; m-= node [I]. f;} else {sum + = m * node [I]. price; M = 0;} return sum;} int main () {int M, N; while (scanf ("% d", & M, & N) & M> = 0 & n> = 0) {for (INT I = 1; I <= N; I ++) {scanf ("% lf ", & node [I]. ja, & node [I]. f); If (node [I]. F = 0) // The maximum value of node [I] by default when 0 f is required for transaction. price = inf; else node [I]. price = node [I]. ja/node [I]. f; // calculate the value of each JavaBean // printf ("Price: %. 4f \ n ", node [I]. price);} Sort (node + 1, node + n + 1, CMP); // For (INT I = 1; I <= N; I ++) // {// printf ("New: % lf \ n", node [I]. ja, node [I]. f); //} double ans = greedy (m, n); printf ("%. 3f \ n ", ANS );}}