D - 長いだけのネクタイ 2 (Just Long Neckties 2) Editorial /

Time Limit: 3 sec / Memory Limit: 2048 MiB

Score: 100 points

Problem Statement

Just Odd Inventions Co., Ltd. is a company renowned for doing "just odd inventions." Here we just call it JOI Company.

To celebrate the 5th anniversary of their flagship product, "Just Long Neckties," JOI Company has invented a new product: "Just Stretchable Neckties." As its name suggests, this new necktie has the unique feature of being able to extend its length indefinitely.

JOI Company has decided to hold a showcase event to promote the new necktie, and you have been selected as the host of the event. At the event, several models wearing the new neckties will appear on stage. Initially, the length of every necktie worn by the models is set to 1.

After that, you will carry out a total of N performances to demonstrate the necktie's stretchable feature to the audience. Each performance is conducted as follows:

  1. First, you ask the audience to call out a number of their choice. Let this number be x.
  2. Next, you decide whether to respond or ignore it.
    • If you choose to respond to it, you must select one of the models on stage whose necktie length is currently less than or equal to x and set the length of that model's necktie to exactly x. (Note that you may select a model whose necktie length is already x.) However, if no model can be selected, the showcase event will fail.
    • If you choose to ignore it, you do nothing.

However, if you ignore the audience's number two or more times in a row, the audience will get angry, and the showcase event will fail.

The number of models on stage, denoted as k (k\geq 1), has not yet been determined. Since hiring models costs considerable money, it is desirable to keep k as small as possible. The minimum value of k required to prevent the showcase event from failing depends on the numbers called out by the audience during the performances. Fortunately, you possess precognitive abilities and have foreseen that the number called out by the audience during the i-th performance (1\leq i\leq N) will be A_i.

Write a program which, given the numbers called out by the audience during the showcase event, calculates the minimum number of models k required to prevent the showcase event from failing.


Input

Read the following data from the standard input.

N
A_1 A_2 \cdots A_N

Output

Write one line to the standard output. The output should contain the minimum number of models k required to prevent the showcase event from failing.


Constraints

  • 2 \leq N \leq 5\,000\,000.
  • 1\leq A_i \leq 21 (1 \leq i \leq N).
  • Given values are all integers.

Subtasks

  1. (10 points) N\leq 15.
  2. (6 points) N\leq 500, A_i \leq 2 (1 \leq i \leq N).
  3. (12 points) N\leq 500, A_i \leq 5 (1 \leq i \leq N).
  4. (18 points) N\leq 500, A_i \leq 15 (1 \leq i \leq N).
  5. (26 points) N\leq 500\,000, A_i \leq 15 (1 \leq i \leq N).
  6. (10 points) N\leq 500\,000.
  7. (18 points) No additional constraints.

Sample Input 1

5
5 3 4 2 1

Sample Output 1

2

When k=2, the showcase event can be conducted as follows, for example:

  • First, two models wearing the new neckties appear on stage. Initially, the length of each model's necktie is 1.
  • During the 1st performance, the audience calls out 5. You ignore this.
  • During the 2nd performance, the audience calls out 3. You respond to this by selecting the first model and setting their necktie length to 3. The necktie lengths of the two models are now 3 and 1, respectively.
  • During the 3rd performance, the audience calls out 4. You respond to this by selecting the first model and setting their necktie length to 4. The necktie lengths of the two models are now 4 and 1, respectively.
  • During the 4th performance, the audience calls out 2. You respond to this by selecting the second model and setting their necktie length to 2. The necktie lengths of the two models are now 4 and 2, respectively.
  • During the 5th performance, the audience calls out 1. You ignore this.

When k=1, the showcase event will always fail. For example, if you respond to the audience's numbers during the 2nd, 3rd, and 4th performances as described above, the only model's necktie length becomes 4 after the 3rd performance. Then, at the 4th performance, you cannot select a model whose necktie length is less than or equal to 2, so the showcase event fails.

Hence, the minimum number of models k required to prevent the showcase event from failing is 2, and the output should be 2.

This sample input satisfies the constraints of Subtasks 1,3,4,5,6, and 7.


Sample Input 2

6
2 1 1 2 2 1

Sample Output 2

1

When k=1, the showcase event can be conducted as follows, for example:

  • First, a model wearing the new necktie appear on stage. Initially, the length of the model's necktie is 1.
  • During the 1st performance, the audience calls out 2. You ignore this.
  • During the 2nd performance, the audience calls out 1. You respond to this by selecting the only model on stage and setting their necktie length to 1.
  • During the 3rd performance, the audience calls out 1. You respond to this by selecting the only model on stage and setting their necktie length to 1.
  • During the 4th performance, the audience calls out 2. You respond to this by selecting the only model on stage and setting their necktie length to 2.
  • During the 5th performance, the audience calls out 2. You respond to this by selecting the only model on stage and setting their necktie length to 2.
  • During the 6th performance, the audience calls out 1. You ignore this.

Note that in the example above, during the 2nd and 3rd performances, a model whose necktie length is already 1 is selected, and their necktie length is set to 1 again. Such a choice, where the necktie length does not change, is also allowed.

Hence, the minimum number of models k required to prevent the showcase event from failing is 1, and the output should be 1.

This sample input satisfies the constraints of all the subtasks.

\newline


Sample Input 3

10
2 4 6 7 4 5 5 3 4 1

Sample Output 3

3

This sample input satisfies the constraints of Subtasks 1,4,5,6, and 7.

配点: 100

問題文

Just Odd Inventions 社は,「ただ奇妙な発明 (just odd inventions)」をすることで知られている会社である. ここでは略して JOI 社と呼ぶ.

JOI 社は,看板商品である「長いだけのネクタイ」発売 5 周年を記念し,新しく「長くなるだけのネクタイ」を開発した. この新型ネクタイの特徴は,その名の通り長さをいくらでも伸ばせることである.

JOI 社は,新型ネクタイの宣伝を目的とした披露会の開催を決定し,その司会にあなたを抜擢した. 披露会ではまず,新型ネクタイを着用した何人かのモデルが舞台上に登壇する. 最初,各モデルが着用しているネクタイの長さはすべて 1 である.

その後,あなたはネクタイの長さを伸ばせる機能を観客に実感してもらうためのパフォーマンスN 回行う. 各パフォーマンスは以下のように行われる.

  1. まず,観客に好きな数を 1 つ唱えてもらう.ここで観客が唱えた数を x とおく.
  2. 次に,司会のあなたはこれに反応するか無視するかを選ぶ.
    • 反応することを選んだ場合,あなたは登壇しているモデルのうち着用しているネクタイの長さが x 以下であるような者を 1 人選び,そのモデルのネクタイの長さを x にする(着用しているネクタイの長さが元々 x であるようなモデルも選ぶことができる点に注意せよ). ただし,選ぶことのできるモデルが 1 人も存在しない場合,披露会は失敗に終わる.
    • 無視することを選んだ場合,何もしない.

ただし,観客の唱えた数を 2 回以上連続で無視してしまうと,観客が機嫌を損ね,披露会は失敗に終わる.

舞台上に登壇させるモデルの人数 k (k\geqq 1) はまだ決まっていないが,モデルを雇うのにはお金がかかるため,k の値はできるだけ小さい方が望ましい. 披露会が失敗に終わらないために必要なモデルの人数は各パフォーマンスで観客が唱える数に依存するが,あなたはその予知能力により,i 回目 (1\leqq i\leqq N) のパフォーマンスで観客が唱える数が A_i であることを予見した.

披露会で観客が唱える数の情報が与えられたとき,披露会が失敗に終わらないために必要なモデルの人数 k の最小値を求めるプログラムを作成せよ.


入力

入力は以下の形式で標準入力から与えられる.

N
A_1 A_2 \cdots A_N

出力

標準出力に,披露会が失敗に終わらないために必要なモデルの人数 k の最小値を 1 行で出力せよ.


制約

  • 2 \leqq N \leqq 5\,000\,000
  • 1\leqq A_i \leqq 21 (1 \leqq i \leqq N).
  • 入力される値はすべて整数である.

小課題

  1. (10 点) N\leqq 15
  2. (6 点) N\leqq 500A_i \leqq 2 (1 \leqq i \leqq N).
  3. (12 点) N\leqq 500A_i \leqq 5 (1 \leqq i \leqq N).
  4. (18 点) N\leqq 500A_i \leqq 15 (1 \leqq i \leqq N).
  5. (26 点) N\leqq 500\,000A_i \leqq 15 (1 \leqq i \leqq N).
  6. (10 点) N\leqq 500\,000
  7. (18 点) 追加の制約はない.

入力例 1

5
5 3 4 2 1

出力例 1

2

k=2 のとき,例えば以下のように披露会を行うことができる.

  • まず,新型ネクタイを着用した 2 人のモデルが舞台上に登壇する.最初,各モデルのネクタイの長さはいずれも 1 である.
  • 1 回目のパフォーマンスでは,観客は 5 を唱える.あなたはこれを無視する.
  • 2 回目のパフォーマンスでは,観客は 3 を唱える.あなたはこれに反応して,1 人目のモデルを選び,そのネクタイの長さを 3 にする.各モデルのネクタイの長さはそれぞれ 3,1 になる.
  • 3 回目のパフォーマンスでは,観客は 4 を唱える.あなたはこれに反応して,1 人目のモデルを選び,そのネクタイの長さを 4 にする.各モデルのネクタイの長さはそれぞれ 4,1 になる.
  • 4 回目のパフォーマンスでは,観客は 2 を唱える.あなたはこれに反応して,2 人目のモデルを選び,そのネクタイの長さを 2 にする.各モデルのネクタイの長さはそれぞれ 4,2 になる.
  • 5 回目のパフォーマンスでは,観客は 1 を唱える.あなたはこれを無視する.

k=1 のとき,披露会は必ず失敗に終わる. 例えば,上記の進行例のように 2,3,4 回目のパフォーマンスで観客の唱えた数に反応した場合,3 回目のパフォーマンスが終了した時点で登壇している唯一のモデルのネクタイの長さが 4 になっているため, 4 回目のパフォーマンスにおいて,着用しているネクタイの長さが 2 以下であるようなモデルを選ぶことができず,披露会は失敗に終わる.

よって,披露会が失敗に終わらないために必要なモデルの人数 k の最小値は 2 であるため,2 を出力する.

この入力例は小課題 1,3,4,5,6,7 の制約を満たす.


入力例 2

6
2 1 1 2 2 1

出力例 2

1

k=1 のとき,例えば以下のように披露会を行うことができる.

  • まず,新型ネクタイを着用した 1 人のモデルが舞台上に登壇する.最初,モデルのネクタイの長さは 1 である.
  • 1 回目のパフォーマンスでは,観客は 2 を唱える.あなたはこれを無視する.
  • 2 回目のパフォーマンスでは,観客は 1 を唱える.あなたはこれに反応して,登壇している唯一のモデルを選び,そのネクタイの長さを 1 にする.
  • 3 回目のパフォーマンスでは,観客は 1 を唱える.あなたはこれに反応して,登壇している唯一のモデルを選び,そのネクタイの長さを 1 にする.
  • 4 回目のパフォーマンスでは,観客は 2 を唱える.あなたはこれに反応して,登壇している唯一のモデルを選び,そのネクタイの長さを 2 にする.
  • 5 回目のパフォーマンスでは,観客は 2 を唱える.あなたはこれに反応して,登壇している唯一のモデルを選び,そのネクタイの長さを 2 にする.
  • 6 回目のパフォーマンスでは,観客は 1 を唱える.あなたはこれを無視する.

なお,上記の進行例における 2,3 回目のパフォーマンスでは,元々ネクタイの長さが 1 であるようなモデルを選んでそのネクタイの長さを 1 にしているが,そのように,ネクタイの長さが変化しないようなモデルの選び方も許されていることに注意せよ.

よって,披露会が失敗に終わらないために必要なモデルの人数 k の最小値は 1 であるため,1 を出力する.

この入力例はすべての小課題の制約を満たす.


入力例 3

10
2 4 6 7 4 5 5 3 4 1

出力例 3

3

この入力例は小課題 1,4,5,6,7 の制約を満たす.