如何证明CodeForces问题的该解决方案的正确性。无聊&quot?
我正在查看CodeForces问题 a。无聊:
给定一个由整数组成的序列。玩家可以做出几个步骤。在一个步骤中,他可以选择序列的一个元素(让我们表示它
I am looking at the Codeforces problem A. Boredom:
Given a sequence ???? consisting of ???? integers. The player can make several steps. In a single step he can choose an element of the sequence (let's denote it ????????) and delete it, at that all elements equal to ????????+1 and ????????−1 must also be deleted from the sequence. That step brings ???????? points to the player.
I found the following code submitted by user Leeisateam:
input()
z = [0] * 7**6
for i in map(int, input().split()):
z[i] += i
a = b = 0
for i in z:
a, b = max(a, i + b), a
print(a)
I understand what this code is doing up until the final loop starts. We're creating a list z
of the form
[0 X count_in_sequence(0), 1 X count_in_sequence(1), ..., n X count_in_sequence(n)].
After that b
is assigned the value of a
and a
uses that (previous) value in the next iteration. I tried induction, but I still can't understand why this code would always work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些观察:
Some observations:
Now let's look at the code.
The code projects the values to their corresponding index, so that they are automatically visited in ascending order.
Then the variables
a
andb
are introduced. They represent two alternative solutions of picking values before the current valuei
is taken (or not). The one represented bya
is one that might have taken the valuei-1
, and so if that would be done, the valuei
would not be available anymore (as deleted by the previous pick). By consequence, we don't consider thea
scenario for pickingi
. On the other handb
represents a variant where we surely did not picki-1
, and so we are sure that after scenariob
we can also pick valuei
. So now after having consideredi
, we have two scenarios:a
(which wasn't extended) andb + i
(note also thati
has all the duplicates summed up as we know we want to take all or nothing of it).In the next iteration of the loop, the role of
a
andb
change, since nowb
has possibly selected that previousi
, which now isi-1
, and that previousb
is nowa
(it is like a swap).And so the loop "alternates" through the
z
array, keeping track of two possible scenarios.Describing this in words makes it quite verbose, but it does help to step through the code with a piece of paper using some limited inputs.
Example visualisation
Let's say we have input with the following (value, frequency) pairs:
Then we could visualise the values for
a
andb
as the loop makes iterations:z
will be equal to:i
represents what extra value can be added when we select the current value -- which is that value multiplied by its frequency in the input. This value is only allowed to be added tob
as the scenario fora
has allowed for the selection of the immediate predecessor value. Still, adding this valuei
tob
is a potential new value fora
, but only if it is better than whata
is. In the example we see this isb+i
not better thana
wheni
is 0 (which is true in the largest part of thez
list... having 76 entries).And at the end
a
has the solution: 296