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. Linux笔记-top命令和load average

    参考资料 top相关: http://blog.csdn.net/zhangchenglikecc/article/details/52103737参考资料 cpu核数: https://www.cn ...

  2. face alignment[Ordinary Procrustes Analysis]

    人脸识别,大致可以分为以下四个步骤: 人脸检测:从图片中准确定位到人脸,并以矩形框将其裁剪出来: 人脸矫正(对齐): 检测到的人脸,可能角度不是很正,需要使其对齐,比如旋转,缩放: 特征提取:对矫正后 ...

  3. 多线程-Callable、Future、FutureTask

    我们普遍知道的创建线程的方式有两种,一种是继承Thread,一种是实现Runnable接口.这两种方式都无法获取任务执行完后的结果,并发包提供了Callable 类能够得到任务执行完的结果. 为何需要 ...

  4. 【原创】研发应该懂的binlog知识(上)

    引言 为什么写这篇文章? 大家当年在学MySQL的时候,为了能够迅速就业,一般是学习一下MySQL的基本语法,差不多就出山找工作了.水平稍微好一点的童鞋呢还会懂一点存储过程的编写,又或者是懂一点索引的 ...

  5. 在IDEA中构建Tomcat项目流程

    在IDEA中构建Web项目流程 打开你的IDEA,跟着我走! 第一步:新建项目 第二步:找到Artifacts 点击绿色的+号,如图所示,点一下 这一步很关键,目的是设置输出格式为war包,如果你的项 ...

  6. MyBatis动态SQL(认真看看, 以后写SQL就爽多了)

    目录 0 一起来学习 mybatis 1 数据准备 2 if 标签 2.1 在 WHERE 条件中使用 if 标签 2.1.1 查询条件 2.1.2 动态 SQL 2.1.3 测试 2.2 在 UPD ...

  7. 使用redis实现生产者消费者模式

    本次主要分享一下使用redis做缓存队列,实现生产者消费者模式. 首先先来看一下redis提供的列表操作接口.像ListRightPush就和符合队列先进先出的原则. 然后围绕这个列表已下单为例简要实 ...

  8. hibernate多对多的更新问题

    错误原因 A different ]; nested exception ]] with root cause org.hibernate.NonUniqueObjectException: A di ...

  9. jQuery基础语法知识梳理

    一.attr() attr()方法设置或返回元素的属性. attr(属性名):获取元素属性名的值. attr(属性名,属性值):设置元素属性名的值. 例子: <a href=”http://12 ...

  10. @SuppressWarnings("resource")

    Suppress  抑制:镇压:废止 Warnings警告 @SuppressWarnings("resource")是J2SE 提供的一个批注.该批注的作用是给编译器一条指令,告 ...