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.