#P16271. [蓝桥杯 2026 省 Java B 组] 擂台赛

    ID: 18291 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 显示难度提高 上传者: 标签>动态规划 DP2026蓝桥杯省赛

[蓝桥杯 2026 省 Java B 组] 擂台赛

Problem Description

Xiao Lan wants to organize an arena match. There are nn players, numbered from 11 to nn. Initially, each player ii is on their own arena ii. Each player ii has exactly one target arena aia_i that they want to go to and challenge.

Before the match starts, Xiao Lan needs to decide an order in which the players will act. Following this order, for each player ii who acts:

  • If their target arena aia_i has not been closed yet, they will leave their own arena ii and go to arena aia_i to challenge, and at the same time close their original arena ii.
  • If their target arena aia_i has already been closed, then they cannot start the challenge and can only stay where they are.

Xiao Lan wants to choose an order so that in the end, the number of arenas that have been used (i.e., successfully visited as a challenge target) is as small as possible. Note that players who stay where they are do not count as using an arena.

Please help Xiao Lan compute: under the best acting order, what is the minimum number of arenas that will be used?

Input Format

The input has 2 lines.

The first line contains a positive integer nn, representing the number of players.

The second line contains nn positive integers a1,a2,,ana_1, a_2, \dots, a_n, representing the target arena number for each player.

Output Format

Output one integer, representing the minimum number of arenas that have been used.

5
2 3 4 5 4
2

Hint

Sample Explanation

One optimal acting order is: 1,3,2,5,41, 3, 2, 5, 4.

  1. Player 11 challenges 22: target arena 22 is not closed, the action succeeds, arena 11 is closed, arena 22 is used;
  2. Player 33 challenges 44: target arena 44 is not closed, the action succeeds, arena 33 is closed, arena 44 is used;
  3. Player 22 challenges 33: target arena 33 was closed when player 33 acted, so player 22 can only stay where they are;
  4. Player 55 challenges 44: target arena 44 is not closed, the action succeeds, arena 55 is closed, arena 44 is used;
  5. Player 44 challenges 55: target arena 55 was closed when player 55 acted, so player 44 can only stay where they are.

In the end, the arenas that have been used are {2,4}\{2, 4\}, a total of 22.

Constraints and Notes

For 30%30\% of the testdata, n10n \leq 10.

For 100%100\% of the testdata, 1n1061 \leq n \leq 10^6, 1ain1 \leq a_i \leq n, and aiia_i \ne i.

Translated by ChatGPT 5