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. 使用RequireJS优化Web应用前端

    require.js官网:http://requirejs.org/docs/download.html 一篇不错的文章:http://www.csdn.net/article/2012-09-27/ ...

  2. html中可以使用在块级元素<body>中的元素

    1.<p></p>当在html页面中需要显示大段文字的时候,可以使用p元素标记每一个段落的边界,需要注意的是,段落是块级元素,只允许包含文本和行内元素. 以下标注的是p中的标准 ...

  3. 第5章标准I/O库总结

    1 fwide函数试图设置流的定向(流的定向决定了读写单字节还是多字节字符) int fwide(FILE *fp,int mode) 宽定向返回正值,字节定向返回负值,为定向返回0 已定向流不会改变 ...

  4. Spark-Cassandra-Connector 插入数据函数saveToCassandra

    在spark-shell中将数据保存到cassandra: var data = normalfill.map(line => line.split("\u0005")) d ...

  5. shell中的条件判断

    read命令 命令的语法: read [参数] 变量名 常用的参数如下: -t timeout 设定超时时间. -p prompt 设定提示信息,该提示信息将会显在光标前. 条件测试: 条件测试可以判 ...

  6. spring mvc 和mybatis整合 的异常处理

    1.自定义异常信息类 通过构造函数来实现异常信息的接收 public class CustomException extends Exception { //异常信息 private String m ...

  7. C语言结构体中的函数指针

      这篇文章简单的叙述一下函数指针在结构体中的应用,为后面的一系列文章打下基础 本文地址:http://www.cnblogs.com/archimedes/p/function-pointer-in ...

  8. 由css属性:vertial-align想到的。。

    我的笔记本:型号 acer4750G-2412g50mnkk 分辨率:1333*768,点距:0.25933mm; 12px下的font-size: 默认line-height减去font-size: ...

  9. Web负载均衡的几种方式

    Web负载均衡的几种实现方式 摘要:负载均衡(Load Balance)是集群技术(Cluster)的一种应用.负载均衡可以将工作任务分摊到多个处理单元,从而提高并发处理能力.目前最常见的负载均衡应用 ...

  10. 第 13 章 装饰模式【Decorator Pattern】

    以下内容出自:<<24种设计模式介绍与6大设计原则>> Ladies and gentlemen,May I get your attention,Please?,Now I’ ...