Hello...I am a dream coder...and now I want to master dynamic programming...recently I solving the following problem....but couldn't understand the editorial.
Could any one pls explain this problem clearly..for a DP beginner.....
Thanks in advance
№ | Пользователь | Рейтинг |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
Страны | Города | Организации | Всё → |
№ | Пользователь | Вклад |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 150 |
Hello...I am a dream coder...and now I want to master dynamic programming...recently I solving the following problem....but couldn't understand the editorial.
Could any one pls explain this problem clearly..for a DP beginner.....
Thanks in advance
Название |
---|
Let f[i] be the maximum sum he can get from taking only values less than or equal i. Then clearly f[0] = 0 since he can't take any of the aray elements. For other values of i, there are two possible cases. The first case is that he does not take any values from the array that are equal to i (sometimes it is better to not take them so he doesn't have to delete all occurrences of i+1 and i-1). In this case, f[i] = f[i-1]. If he doesn't take any values equal to i, the maximum sum he gets is no different than the maximum sum when he was only allowed to take values up to i-1. The other case is that he does take all occurrences of the value i (it's never optimal to take some but not all occurrences since he has to delete the other elements either way). In this case, he is not allowed to take any occurrences of i-1 since he has to delete them all, but he does get the benefit of adding i * freq[i] to his score (where freq[i] is the number of occurrences of i in the array). The fact that he has to delete all occurrences of i+1 will be taken care of when calculating f[i] so you don't have to worry about that. So in this case f[i] = i * freq[i] + f[i-2]. So when figuring out f[i], you take whichever of these cases results in a higher result. Here is some pseudo-code (assuming you pre-computed freq[i] for all i up to 100,000):
f[0] = 0;
for(int i = 1; i<=100000; i++)
{
f[i] = f[i-1];
if(i > 1) f[i] = Math.max(f[i], f[i-2] + i*freq[i]); // Watch out for overflow here.
}
Sir...how will I+1 be taken care byf(I)
The recurrence I think it's like this:
So you can take the
i
items and move toi+2
because thei+1
will be removed, or you will not take thei
items and move to tryi+1
:) I hope it helps!