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.

代码:

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+3;
int vis[MAXN];
int main()
{
memset(vis,-1,sizeof(vis));
int n,x;
cin>>n;
int res=0;
for(int i=0;i<n;i++){
cin>>x;
if(x!=i) vis[i]=x;
else res++;
}
//cout<<res<<endl;
int flag=0;
for(int i=0;i<n;i++){
if(vis[i]!=-1){
if(vis[vis[i]]!=-1){
flag=1;
}
if(vis[vis[i]]==i){
flag=2;
break;
}
}
}
//cout<<flag<<endl;
// for(int i=0;i<n;i++){
// cout<<vis[i]<<"\t";
// }
//cout<<endl;
cout<<res+flag<<endl;
}

codefroces Round #201.B--Fixed Points的更多相关文章

  1. codefroces Round #201.a--Difference Row

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description You wa ...

  2. B. Fixed Points

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

  3. codeforces B.Fixed Points

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

  4. Educational Codeforces Round 46 C - Covered Points Count

    C - Covered Points Count emmm 好像是先离散化一下 注意 R需要+1 这样可以确定端点 emmm 扫描线?瞎搞一下? #include<bits/stdc++.h&g ...

  5. Educational Codeforces Round 64 C. Match Points 【二分思想】

    一 题面 C. Match Points 二 分析 根据题意很容易想到要去找满足条件的数,因为可以打乱输入的顺序,所以很容易想到二分. 但是如果直接对输入的数组进行二分,如输入$a$,直接在数组里二分 ...

  6. codeforces B. Fixed Points 解题报告

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

  7. codeforces round #201 Div2 A. Difference Row

    #include <iostream> #include <vector> #include <algorithm> using namespace std; in ...

  8. codeforce Codeforces Round #201 (Div. 2)

    cf 上的一道好题:  首先发现能生成所有数字-N 判断奇偶 就行了,但想不出来,如何生成所有数字,解题报告 说是  所有数字的中最大的那个数/所有数字的最小公倍数,好像有道理:纪念纪念: #incl ...

  9. Codeforces Round #201 (Div. 2) - C. Alice and Bob

    题目链接:http://codeforces.com/contest/347/problem/C 题意是给你一个数n,然后n个数,这些数互不相同.每次可以取两个数x和y,然后可以得到|x - y|这个 ...

随机推荐

  1. springboot笔记-使用JSP

    Spring Boot 项目中使用 JSP: 项目结构:需要添加webapp文件夹用来存放目录 jsp 文件 spring-boot-jsp +-src +- main +- java +- reso ...

  2. PHP空对象 空数组

    PHP定义空对象:$obj = (object)null;或$obj = (object)array(); 定义空数组:$arr = array();或$arr = [];//自 5.4 起可以使用短 ...

  3. Android开发build出现java.lang.NumberFormatException: For input string: "tle 0x7f0800aa"错误的解决方案

    查看异常栈没有发现项目代码的问题,因为问题是出现在layout文件中. 全局查找tle这个,发现在某个layout文件中title一词被变成ti tle了,结果Android就xjb报错了. 参考

  4. 2016 计蒜之道 初赛 第一场 D 青云的机房组网方案 (虚树)

    大意: 给定树, 点$i$的点权为$a_i$, 求$\sum\limits_{a_i \perp a_j}dis(i,j)$ 中等难度可以枚举每条边的贡献, 维护子树内每个数出现次数$a$, 转化为求 ...

  5. eclipse+maven搭建springboot项目入门

    一.下载jdk,例如(jdk1.8.171) 安装(注意仅仅安装jdk就可以了,不要安装jre,设置JAVA_HOME,配置jdk环境变量) 二.下载maven(apache-maven-3.5.3- ...

  6. Chrome开发者工具详解(一)之使用断点来调试代码上

    1.断点调试是啥?难不难? 断点调试其实并不是多么复杂的一件事,简单的理解无外呼就是打开浏览器,打开sources找到js文件,在行号上点一下罢了.操作起来似乎很简单,其实很多人纠结的是,是在哪里打断 ...

  7. 分布式的几件小事(六)dubbo如何做服务治理、服务降级以及重试

    1.服务治理 服务治理主要作用是改变运行时服务的行为和选址逻辑,达到限流,权重配置等目的. ①调用链路自动生成 一个大型的分布式系统,会由大量的服务组成,那么这些服务之间的依赖关系和调用链路会很复杂, ...

  8. xampp for mac配置局域网访问

    下载了xampp,之前在window上面经常用.后来用了mac下载xampp后,配置局域网总是没成功.现在配置好了,来和大家分享一下配置局域网访问步骤. 1. 下载 下载地址:https://www. ...

  9. ubuntu自己定义环境变量,替代常用的操作命令

    问题背景是这样的,因为自己会经常用自己的用户链接服务器,比如自己的用户是yongjie,然后服务器的ip是162.105.97.31 所以经常执行的命令是ssh yongjie@162.105.97. ...

  10. SpringMVC @Valid,@RequestBody,@RequestParam标注参数时,进行Postman测试

    @Valid(post请求) 可与@RequestBody一起使用 > (@RequestBody @Valid User user) @RequestBody(post请求) 这里的requi ...