Some backpack problems, greedy
Sort by J [I]/f [I] in ascending order. Select the maximum value of J [I]/f [I] each time. take the capacity that fills the backpack. The reason is very simple. Each time J [I]/f [I] is taken, the maximum volume value of the backpack is the most. From the largest to the smallest choice, you will naturally get the optimal solution.
#include
#include
# include
using namespace STD; const int n = 1005; struct info {Int J, F; double divid ;}; info A [n]; bool CMP (const info & A, const info & B) {return. divid> B. divid;} int main () {int M, N; while (scanf ("% d", & M, & n )) {If (M =-1 & n =-1) break; For (INT I = 1; I <= N; I ++) {scanf ("% d", & A [I]. j, & A [I]. f); A [I]. divid = (double) A [I]. j/A [I]. f;} int beans = 0, catfood = 0; int I; sort (a + 1, A + n + 1, CMP); for (I = 1; I <= N; I ++) {beans + = A [I]. j; catfood + = A [I]. f; If (catfood> m) {catfood-= A [I]. f; beans-= A [I]. j; break ;}}if (I <= N) Beans + = (double) (m-catfood)/A [I]. f * A [I]. j; printf ("%. 3lf \ n ", beans);} return 0 ;}