Title Description
There are \ (n\) items, each item has a volume \ (v_i\), backpack capacity \ (s\). Ask to select items that are packed with backpacks and have the fewest number of items, and in this scenario:
(1) Find the median of the least median (\ (k\) Elements of the median is from small to large number of ⌊k/2⌋\);
(2) The majority of the programs with the smallest number of outstanding numbers;
(3) To find the extreme difference of the minimum scheme.
Thinking of solving problems
The value of each item is 1, to fill the minimum value, this is only 01 backpack, the answer is the minimum number \ (m\).
For the majority, the two-point answer and delete the excess items can be.
For the median, two-point answer, so that each item value is \ (inf+t\), where the volume is greater than the answer of the two (t\) is 1, otherwise-1. Also find the minimum value when full, if more than \ (M\times inf\) The real answer is greater, otherwise smaller. Correctness is because at any moment, \ (dp_i\) The value of the preservation if the requirements of the best, first of all to ensure that the number is the fewest (otherwise meaningless), in this condition as little as possible to take large volume of goods. The global optimal answer must be transferred by local optimal answer (which can be proved to be disproved).
For the very poor, consider adding items from small to large, the value of each item (inf\), but if the item is added to the backpack for the first time value \ (inf-v_i\). After each additional item \ (i\) is updated \ (dp\), ask \ (dp_s+v_i\). All values can be minimized.
Time Complexity \ (O (ns \log n) \)
Some feelings
According to the above ideas, in addition to the very poor part can be converted to normal 01 backpack, greatly reducing the amount of code (only 60 lines). The very poor part is also very good to write, see the code.
Unfortunately, the game didn't come out! After a few days after the game after the problem to think for a while to come out (angry ~). Finally, the quality of this problem is really too high! "Good choice!" Moderate difficulty, covering a wide range of knowledge, topics and practical background, the solution is more natural. Praise the person who gave the quiz! ”
AC Code
1#include <cstdio>2#include <algorithm>3#include <cstring>4 using namespacestd;5 #defineMAXV 50016 #defineINF 100017 intDP[MAXV];8 intv[5001], c[5001];9 intSolveintNints)Ten { OneMemset (DP,0x3f,sizeof(DP)); Adp[0] =0; - for(inti =0; I < n; i++){ - for(intj = S; J >= V[i]; j--) theDp[j] = min (Dp[j], Dp[j-v[i]] +c[i]); - } - returnDp[s]; - } + intSolve2 (intNints) - { +Memset (DP,0x3f,sizeof(DP)); Adp[0] =0; at intAns =0x3fffffff; - for(inti =0; I < n; i++){ - for(intj = S; J > V[i]; j--) -Dp[j] = min (Dp[j], Dp[j-v[i]] +INF); -Dp[v[i]] = min (Dp[v[i]], INF-v[i]); -ans = min (ans, dp[s] +v[i]); in } - returnAns%INF; to } + intMain () - { the intN, S; *scanf"%d%d", &n, &s); $ for(inti =0; I < n; i++){Panax Notoginsengscanf"%d", &v[i]); -C[i] =1; the } +Sort (V, v +n); A intnum =solve (n, s), L, R; the if(Num > N) {printf ("-1");return 0; } +printf"%.9LF", (Double) S/num); - for(L =0, R = N-1; L! =R;) { $ intMid = (L + r) >>1, W =V[mid]; $ for(inti =0; I < n; i++) -C[i] = INF + (V[i] > w?1: -1); - if(Solve (n, s) > INF * num) L = mid +1; the ElseR =mid; - }Wuyiprintf"%d", V[l]); the for(L =1, r = N; L! =R;) { - intMid = (L + r) >>1; Wu for(inti =0, J; I < n; i++){ -j =!i | | V[i]! = v[i-1] ?1: J +1; AboutC[i] = J <= mid?1: INF; $ } - if(Solve (n, s) > num) L = mid +1; - ElseR =mid; - } Aprintf"%d", L); +printf"%d", Solve2 (n, s)); the return 0; -}
A backpack motif-petrozavodsk Winter-2018. Carnegie Mellon U Contest problem I