QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 1024 MB Total points: 100

#23360. 相隔最遠的卡片

Statistics

有 $2N$ 張寫有正整數的卡片。這些卡片從左到右排成一行。

每張卡片上恰好寫有一個不小於 $1$ 且不大於 $N$ 的正整數。記從左數第 $i$($1\le i\le2N$)張卡片上的正整數為 $X_i$。

對於每個滿足 $1\le k\le N$ 的 $k$,恰好有兩張卡片寫著 $k$。也就是說,從 $1$ 到 $N$ 的每個正整數都恰好寫在兩張卡片上。

정올이 把兩張寫著正整數 $k$ 的卡片之間的卡片數量稱為「$k$ 之間的卡片數」。

例如,考慮下圖中的卡片排列。圖中 $N=4$,且 $X_1=1,X_2=2,X_3=2,X_4=4,X_5=3,X_6=1,X_7=3,X_8=4$。

problem_23360_66589eed6795aaf5a2e5f52e4af46ceb.png
  • 兩張寫著 $1$ 的卡片之間依序有寫著 $2,2,4,3$ 的卡片,所以「$1$ 之間的卡片數」為 $4$。
  • 兩張寫著 $2$ 的卡片之間沒有卡片,所以「$2$ 之間的卡片數」為 $0$。
  • 兩張寫著 $3$ 的卡片之間只有寫著 $1$ 的卡片,所以「$3$ 之間的卡片數」為 $1$。
  • 兩張寫著 $4$ 的卡片之間依序有寫著 $3,1,3$ 的卡片,所以「$4$ 之間的卡片數」為 $3$。

在上面的例子中,所有「$k$ 之間的卡片數」中的最大值是「$1$ 之間的卡片數」,其值為 $4$。

정올이 想求出從 $1$ 到 $N$ 的所有正整數 $k$ 對應的「$k$ 之間的卡片數」的最大值。

按卡片的排列順序給出各張卡片上的正整數,請撰寫程式,求出所有「$k$ 之間的卡片數」的最大值。

資料範圍

  • 所有給定的數都是整數。
  • $1\le N\le2\,000$
  • 對於每個 $i$ ($1\le i\le2N$): $1\le X_i\le N$
  • 對於每個 $k$ ($1\le k\le N$), 該數恰好出現兩次,所在序列為 $X_1,X_2,\ldots,X_{2N}$.

子任務

  1. (10 分) $N\le2$
  2. (15 分) 答案為 $0$ 或 $1$.
  3. (15 分) 答案為 $2N-3$ 或 $2N-2$.
  4. (20 分) $N\le500$
  5. (40 分) 沒有額外限制。

輸入格式

第一行給出整數 $N$。

第二行給出 $2N$ 個整數 $X_1,X_2,\ldots,X_{2N}$,以空格分隔。

輸出格式

在第一行輸出答案。

範例

輸入格式 1

4
1 2 2 4 3 1 3 4

輸出格式 1

4

輸入格式 2

4
1 2 3 4 4 3 2 1

輸出格式 2

6

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.