Fixed Points

CodeForces - 347B

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 nintegers 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

sol:容易发现交换最多使得答案增加2,一般都能加1(似乎并没什么用)
于是O(n)扫一遍能加2就加2,否则试试能不能加1
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,a[N],Pos[N];
int main()
{
int i,ans=;
R(n);
for(i=;i<=n;i++)
{
a[i]=(read()+); Pos[a[i]]=i;
if(a[i]==i) ans++;
}
for(i=;i<=n;i++) if(a[i]!=i)
{
if(Pos[Pos[a[i]]]==a[i])
{
Wl(ans+);
return ;
}
}
Wl(min(ans+,n));
return ;
}
/*
input
5
0 1 3 4 2
output
3
*/
 

codeforces347B的更多相关文章

随机推荐

  1. 【P1941】 飞扬的小鸟

    题目描述 游戏界面是一个长为 nn,高为 mm 的二维平面,其中有 kk 个管道(忽略管道的宽度). 小鸟始终在游戏界面内移动.小鸟从游戏界面最左边任意整数高度位置出发,到达游戏界面最右边时,游戏完成 ...

  2. Git中删除指定文件

    之前的博客Git基础使用教程介绍了Git这个开源分布式管理系统的一些基础操作,这篇博客,介绍下如何利用Git删除远程仓库的文件... 1.拉取远程仓库的文件到本地 git clone xxx 如果还未 ...

  3. python:利用configparser模块读写配置文件

    在自动化测试过程中,为了提高脚本的可读性和降低维护成本,将一些通用信息写入配置文件,将重复使用的方法写成公共模块进行封装,使用时候直接调用即可. 这篇博客,介绍下python中利用configpars ...

  4. Service启动,绑定与交互

    1. Service的启动方式有startServcie和bindService两种. startService时,会经历onCreate—onStartCommand—onDestroy生命周期, ...

  5. 从源码看Spring Security之采坑笔记(Spring Boot篇)

    一:唠嗑 鼓捣了两天的Spring Security,踩了不少坑.如果你在学Spring Security,恰好又是使用的Spring Boot,那么给我点个赞吧!这篇博客将会让你了解Spring S ...

  6. 字符串的查找KMP

    基本思想,当出现不匹配的时候,就知晓一部分文本内容(因为在匹配失败前已经发生匹配) P[0 ~ k-1] == P[j-k ~ j-1] //KMP #include<iostream> ...

  7. eclipse如何添加web dynamic project

    很多eclipse版本是不能直接新建web dynamic project的,需要从网上找插件或更新. 比较方便的是在Help → Install-New-Software,点击add按钮,在Loca ...

  8. haoop笔记

    : //:什么是hadoop? hadoop是解决大数据问题的一整套技术方案 :hadoop的组成? 核心框架 分布式文件系统 分布式计算框架 分布式资源分配框架 hadoop对象存储 机器计算 :h ...

  9. 在Linux添加PYTHONPATH方法以及修改环境变量方法

    Linux下设置环境变量有三种方法,一种用于当前终端,一种用于当前用户,一种用于所有用户: 一:用于当前终端: 在当前终端中输入: export PATH=$PATH:<你的要加入的路径> ...

  10. use redis instance in docker hub

    redis - Docker Hubhttps://hub.docker.com/_/redis