B. Fixed Points
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Sample test(s)
input
5
0 1 3 4 2
output
3

思路:a[i] = k,且a[k] = i,即i==a[a[i]](k与i不相等)则可增加2,否则若sum!=n,则必增加1.

#include<stdio.h>
#include<math.h>
int a[100001];
int main()
{
int n,i,sum,flag,flag1,p;
while(~scanf("%d",&n))
{
p = flag = flag1 = sum = 0;
for(i = 0;i < n;i ++)
{
scanf("%d",&a[i]);
if(i==a[i])
sum++;
}
if(sum!=n)
flag1 = 1;
for(i = 0;i < n;i ++)
{
if(a[a[i]]==i&&a[i]!=i)
{
flag = 1;
break ;
}
}
if(!(flag+flag1))
printf("%d\n",sum);
if(flag==1)
{
printf("%d\n",sum+2);
continue ;
}
if(flag==0&&flag1==1)
printf("%d\n",sum+1);
}

B. Fixed Points的更多相关文章

  1. codeforces B.Fixed Points

    link:http://codeforces.com/contest/347/problem/B 很简单,最多只能交换一次,也就是说,最多会增加两个.可能会增加一个.也可能一个也不增加(此时都是fix ...

  2. codeforces B. Fixed Points 解题报告

    题目链接:http://codeforces.com/problemset/problem/347/B 题目意思:给出一个包含n个数的排列a,在排列a中最多只能作一次交换,使得ai = i 这样的匹配 ...

  3. [Codeforces] 347B - Fixed Points

    题意:给定一个序列,现有一种操作:两个数的位置互换.问最多操作一次.序列 [元素位置i]  与 [元素Ai] 相等的最多个数? 依据题意,最多个数为 : [操作之前[元素位置i]  与 [元素Ai] ...

  4. cf B. Fixed Points

    http://codeforces.com/contest/347/problem/B #include <cstdio> #include <cstring> #includ ...

  5. CodeForces 347B Fixed Points (水题)

    题意:给定 n 数,让你交换最多1次,求满足 ai = i的元素个数. 析:很简单么,只要暴力一遍就OK了,先把符合的扫出来,然后再想,最多只能交换一次,也就是说最多也就是加两个,然后一个的判,注意数 ...

  6. 不动点(Fixed Point)

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/51735818 在数学中,函数的不动点( ...

  7. codefroces Round #201.B--Fixed Points

    B. Fixed Points time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  8. (转) Summary of NIPS 2016

    转自:http://blog.evjang.com/2017/01/nips2016.html           Eric Jang Technology, A.I., Careers       ...

  9. 东大OJ-一元三次方程解的个数

    1043: Fixed Point 时间限制: 5 Sec  内存限制: 128 MB 提交: 26  解决: 5 [提交][状态][讨论版] 题目描述 In mathematics, a fixed ...

随机推荐

  1. CSS背景属性

    CSS背景属性 1.background-attachment 属性 scroll:默认值.背景图像会随着页面其余部分的滚动而移动. fixed:当页面的其余部分滚动时,背景图像不会移动. inher ...

  2. 讲解最好的Python正则表达式

    这篇文章转载自:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html#!comments 这里非常感谢作者无私的贡献自己的成果. 请大 ...

  3. Qt文件信息获取之QFileInfo

    在Qt中为文件的操作和信息获取提供了许多方便的类,常用的有QDir,QFile,QFileInfo以及QFileDialog,在本文中主要介绍用于获取关于文件信息的QFileInfo类. QFileI ...

  4. placeholder属性兼容js支持

    $(function(){ //判断浏览器是否支持placeholder属性 supportPlaceholder='placeholder'in document.createElement('in ...

  5. css实现的透明三角形

    css实现下图样式,具体像素值记不住了,很好设置,html code (2014百度秋招面试题): <div id="demo"></div>   分析:这 ...

  6. 一分钟搭建Webpack+react+es6框架

    最近react刷屏的厉害,而随着它一起走进我们视野的还有webpack,webpack只是个工具,为什么如此火呢?因为简单好了不废话.   直接进入正题: 打开命令行工具: npm install - ...

  7. MySQL在远程访问时非常慢的解决skip-name-resolve 并且出现 Reading from net

    转载:http://www.itokit.com/2012/0515/73932.html 服务器放在局域网内进行测试时,数据库的访问速度还是很快.但当服务器放到外网后,数据库的访问速度就变得非常慢. ...

  8. delete删除多表

    1.DELETE a.*, aa.* FROM student a, person aa WHERE a.id = aa.city_id AND a.name = '' 2.DELETE a.*, a ...

  9. 《疯狂java讲义》笔记 1-5章

    1.编译语言和解释语言理解,摘自李刚老师的<疯狂Java讲义>第三版: 就是说,Java和.net都是编译型有事解释型语言.编译型就是根据不同平台编译成不同的可执行机器码,编译过程中会进行 ...

  10. tomcat https 配置

    以前基本上笔者对于安全性考虑的并不多,最近因为saas平台要开始逐渐推广,所以需要开始逐渐加强xss/crsf/https等措施以避免潜在的安全性风险.本文简单的记录下tomcat下https的配置. ...