Greedy algorithm
If you define a comparison function for sort in a class, be careful to add static before comparing the function. Description The function is a static function that occupies a fixed memory in memory.
1#include <cstdio>2#include <algorithm>3 using namespacestd;4 5 #defineREP (n) for (int o=0;o<n;o++)6 7 8 classLove {9 Private:Ten Static Const intMAXN = -; One intM, N; A structNode { - intT, V; - Doublep; the }PAPER[MAXN]; - - Static BOOLCompare_node (Node A, Node B) { - returnA.P >B.P; + } - + Public: A BOOLDo () { atscanf"%d%d", &m, &N); - if(M = =0&& N = =0)return false; - REP (M) { - intT, V; -scanf"%d%d", &t, &V); -Paper[o]. T =T; inPaper[o]. V =V; -PAPER[O].P = (Double) V/(Double) T; to } + -Sort (paper, paper +M, Compare_node); the * //REP (M) $ //printf ("t=%d v=%d p=%f\n", Paper[o]. T, Paper[o]. V, PAPER[O].P);Panax Notoginseng - DoubleAns =0; the for(inti =0; i < M; i++) { + inttemp = N-Paper[i]. T; AAns + =Paper[i]. V; the if(Temp <0) { +Ans + = temp*PAPER[I].P; - Break; $ } $N =temp; - } - theprintf"%.2f\n", ans); - return true;Wuyi } the }; - Wu intMain () { - Love LIVE; About while(LIVE. Do ()); $ return 0; -}
AOJ 2.Home Work