This question is just full of knapsack problem, just choose K, the state transfer equation is dp[i][j] = max (Dp[i-1][j], dp[i-1][j-1] + li-bi (j-1))
DP[I][J] means the first I boys to choose the maximum value of J Li, firstly according to BI row order, this is the use of greedy ideas, because that can get the best solution, in descending order to find the maximum value, and then DP, the state transfer equation meaning is the first J to go or not to take, the code As follows
Code one (two-dimensional array version):
1#include <stdio.h>2#include <iostream>3#include <string.h>4#include <algorithm>5 using namespacestd;6 structhappy{7 intLi, Bi;8 };9 Const intN =1002;Ten intDp[n][n]; One Happy Happy[n]; A BOOLCMP (Happy A, Happy b)//Descending sort - { - returnA.bi >B.bi; the } - intMain () - { - intN, v; + while(~SCANF ("%d%d", &n, &v)) - { +Memset (DP,0,sizeof(DP)); A for(inti =1; I <= N; i++) atscanf"%d", &Happy[i]. Li); - for(inti =1; I <= N; i++) -scanf"%d", &Happy[i]. Bi); -Sort (Happy +1, Happy + n +1, CMP);//Greedy thought - for(inti =1; I <= N; i++) - { in for(intj =1; J <= I && J <= v; J + +) -DP[I][J] = max (Dp[i-1][J], Dp[i-1][j-1] + happy[i]. Li-happy[i]. Bi * (J-1)); to } +printf"%d\n", Dp[n][v]); - } the * return 0; $}
Code two (optimized space edition):
1#include <stdio.h>2#include <iostream>3#include <string.h>4#include <algorithm>5 using namespacestd;6 structhappy{7 intLi, Bi;8 };9 Const intN =10002;Ten intDp[n]; One Happy Happy[n]; A BOOLCMP (Happy A, Happy b)//Descending sort - { - returnA.bi >B.bi; the } - intMain () - { - intN, v; + while(~SCANF ("%d%d", &n, &v)) - { +Memset (DP,0,sizeof(DP)); A for(inti =1; I <= N; i++) atscanf"%d", &Happy[i]. Li); - for(inti =1; I <= N; i++) -scanf"%d", &Happy[i]. Bi); -Sort (Happy +1, Happy + n +1, CMP);//Greedy thought - for(inti =1; I <= N; i++) - { in for(intj = V; J >=1; j--) - /*This sentence is equivalent to dp[j] = max (Dp[j], dp[j-1] + happy[i]. Li-happy[i]. Bi * (j-1)), but with if faster*/ to if(Dp[j-1] + happy[i]. Li-happy[i]. Bi * (J-1) >Dp[j]) +DP[J] = dp[j-1] + happy[i]. Li-happy[i]. Bi * (J-1 ); - } theprintf"%d\n", Dp[v]); * } $ Panax Notoginseng return 0; -}
HDU-2670 Girl Love Value