|
|
add the time cutoff to speed up It seems, the only solution to this problem is recursion with correct "guess" on how to implement it to avoid TL. dp[n][k] = max{dp[n-1][k] or a[n], dp[n-1][k-1]} any hints?? thank you. Edited by author 18.01.2017 13:16 3 2 7 6 1 Answer: 7 3 2 6 1 7 Answer: 7 because DP like this doesn't work lets consider next case 3 1 90 75 20 when we calculate last step we have: dp[3][1] = max(dp[2][1] or 20, dp[2][0]) now dp[2][1] = 90, dp[2][0] = 90 or 75 = 91 we are choosing max between (90 or 20) and 91, which is equal to 94 but this is incorrect answer correct answer is 75 or 20 = 95 Could someone provide any hint? Edited by author 10.06.2017 23:14 Here's a brainless but simple way i did it. 0. Mini-optimization: remove all repeated numbers, leaving only unique ones, and reduce k respectively. Let A be the array with those unique numbers. 1. Make a list of indices that point to numbers which have 1st bit 1, 2nd bit 1, ..., 32nd bit 1. Like for example: L[1..32][0..nmax], L[5][0] — amount of numbers which have 1 on 5th bit, A[L[5][1]], A[L[5][2]], ... — those numbers. 2. Main algo. Make current_result = 0. Going from higher bit to lower (i = 1..32), if list is not empty (L[i][0] > 0) and ith bit is 0 in current_result, then choose random index j, and do current_result = current_result or L[i][j]. Increase i and repeat either till you reach the last bit or reach the limit of numbers to be deleted. Then update the best reached result. 3. Spam step 2 for a certain time. I initially decided to consume entire 2s, but apparently even 200ms is enough to ac. Clever way is DP, yeah, but requires thinking :) I have tried with a greedy algorithm but gives WA#8, any tests pls? 3 1 2 5 6 Answer 7. Yep, my answer is also 7. Still WA#8 here another tests for my program, can u check pls? 5 3 1245 5553 3112 677 1 answer -> 7609 8 2 12455 6666 3312 245 3432 666 421 444 answer -> 16383 Hard to tell your problem without a code. Greedy algorithm is wrong. Try this: 4 1 16 10 9 6 The answer should be 31 because 16 or 9 or 6 is 31. But the greedy algorithm gives 30. Manciu Ion , yes, for me is 15. For mms: Mine gives 31 too Edited by author 15.03.2017 19:44 |
|
|