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. c# 串口发送接收数据

    /********************** 串口数据接收事件 *****************************/ private void SerialPort_DataReceived ...

  2. 在jsp中选中checkbox后 将该记录的多个数据获取,然后传到Action类中进行后台处理 双主键情况下 *.hbm.xml中的写法

    在jsp中选中checkbox后 将该记录的多个数据获取,然后传到Action类中进行后台处理 双主键情况下 *.hbm.xml中的写法   ==========方法1: --------1. 选相应 ...

  3. awk 查找文件长度 删除

    #在某个目录下,由于有些是缓存文件,它们的共同点就是长度大于3, 找到它们,然后用rm 命令删除#ls abc.pyabcd.py.... #ls | awk 'length($1) > 3 { ...

  4. Ubuntu下Java环境配置

    Oracle Java安装: 通过以下命令进行安装: sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt ...

  5. 浏览器信息(Navigator)

    window.navigator 代表正在使用的浏览器的对象. ◆ window.navigator.appCodeName 返回浏览器的代码名."Mozilla" 本来是 Net ...

  6. sass进阶篇总结一

    一.@if 指令: @if 指令是一个 SassScript,它可以根据条件来处理样式块,如果条件为 true 返回一个样式块,反之 false 返回另一个样式块.在 Sass 中除了 @if 之,还 ...

  7. STM32的GPIO

    一.I/O端口位的基本结构 二.端口位配置表 参考:STM32芯片参考手册

  8. 用powerdesigner建模工具生成数据库

    1,使用powerdesignerP:主键 F:外键 M:是否可为空 2,两张表建立主外键关系时,主外键名称要相同.比如A表 A_id name  说明id(P) B表 B_id name  说明id ...

  9. iOS 常用基础框架

    框架名称 功能 Foundation 提供OC的基础类(像NSObject).基本数据类型等 UIKit 创建和管理应用程序的用户界面 QuartzCore 提供动画特效以及通过硬件进行渲染的能力 C ...

  10. Android Camera 预览图像被拉伸变形的解决方法【转】

    问题描述: 预览图像被拉伸变形 问题原因: 由于预览图像大小跟SurfaceView 大小不一致引起 解决方法: 获取系统支持的所有预览尺寸[getSupportedPictureSizes],然后再 ...