经典数据结构思维题:AGC017C - Snuke and Spells

https://atcoder.jp/contests/agc017/tasks/agc017_c

Problem Statement

There are \(N\) balls in a row. Initially, the \(i\)-th ball from the left has the integer \(A_i\) written on it.

When Snuke cast a spell, the following happens:

  • Let the current number of balls be \(k\). All the balls with \(k\) written on them disappear at the same time.

Snuke's objective is to vanish all the balls by casting the spell some number of times. This may not be possible as it is. If that is the case, he would like to modify the integers on the minimum number of balls to make his objective achievable.

By the way, the integers on these balls sometimes change by themselves. There will be \(M\) such changes. In the \(j\)-th change, the integer on the \(X_j\)-th ball from the left will change into \(Y_j\).

After each change, find the minimum number of modifications of integers on the balls Snuke needs to make if he wishes to achieve his objective before the next change occurs. We will assume that he is quick enough in modifying integers. Here, note that he does not actually perform those necessary modifications and leaves them as they are.

Constraints

  • \(1 \leq N \leq 200000\)
  • \(1 \leq M \leq 200000\)
  • \(1 \leq A_i \leq N\)
  • \(1 \leq X_j \leq N\)
  • \(1 \leq Y_j \leq N\)

Answer

先考虑一个较简单的问题:只需要判断答案是否为\(0\)的话,怎么做?

手玩几个样例之后,可以发现一点规律:每次操作时,必定存在大小为当前数的个数\(N\)的数,且\(N-N\text{的个数}\)必定等于次小的数,以此类推。

但问题在于,如何让计算机快速判断呢?

稍加思考,可能能够得到这一种构造:考虑一个线段\([0,N]\),对于每一个出现\(N_i\)次的数\(i\),覆盖\([i-N_i,i]\)即可。若没有未覆盖的区间,则答案为\(0\)

进一步思考可以发现,答案实际上就是为被覆盖的区间的长度!而又因为这种构造方式是容易维护的,至此我们已经\(O(N+M)\)地解决了这题。

https://atcoder.jp/contests/agc017/submissions/21266828

posted @ 2021-03-26 21:01  frank3215  阅读(109)  评论(0编辑  收藏  举报