5952: 固定点

内存限制:256 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:3 通过:3

题目描述



长度为 n 的排列是一个整数序列,使得从 0 到 (n-1) 的每个整数在其中只出现一次。例如,序列 [0,2,1] 是长度为 3 的排列,而 [0,2,2] 和 [1,2,3] 都不是。
函数的不动点是由函数映射到自身的点。置换可以看作是双射函数。我们将得到排列中不动点的定义。整数 i 是排列 a0,a1,...,an-1 的不动点,当且仅当 ai=i。例如,排列 [0,2,1] 有 1 个不动点,置换 [0,1,2] 有 3 个不动点。
你被赋予排列 a。您最多可以交换一次排列的两个元素。您的任务是最大化结果排列中的固定点数。请注意,您最多只能进行一次交换操作。

A permutation of length n is an integer sequence such that each integer from 0 to (n-1) appears exactly once in it. For example, sequence [0,2,1] is a permutation of length 3 while both [0,2,2] and [1,2,3] are not.
A fixed point of a function is a point that is mapped to itself by the function. A permutation can be regarded as a bijective function. We'll get a definition of a fixed point in a permutation. An integer i is a fixed point of permutation a0,a1,...,an-1 if and only if ai=i. For example, permutation [0,2,1] has 1 fixed point and permutation [0,1,2] has 3 fixed points.
You are given permutation a. You are allowed to swap two elements of the permutation at most once. Your task is to maximize the number of fixed points in the resulting permutation. Note that you are allowed to make at most one swap operation.
Input
The first line contains a single integer n (1≤n≤105). The second line contains n integers a0,a1,...,an-1 − the given permutation.
Output
Print a single integer − the maximum possible number of fixed points in the permutation after at most one swap operation.
Examples
Input
5
0 1 3 4 2
Output
3

输入格式

第一行包含单个整数 n (1≤n≤105)。第二行包含 n 个整数 a0,a1,...,an-1 − 给定的排列。

输出格式

打印单个整数 − 最多一次交换操作后排列中最大可能的固定点数。
例子
输入
5
0 1 3 4 2
输出
3

输入样例 复制

5
0 1 3 4 2

输出样例 复制

3

分类标签