小心使得万年船,特别数据别样烦

首先我想说,很多题目真的是就差这么一点点 ,感觉自己哪里都没错,其实就是想不到一些特别的数据
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.
Examples
Input

5
0 1 3 4 2

Output

3

#include <iostream>
#include <algorithm>
using namespace std;
int a[123456];
int main()
{
    int n;
    cin>>n;
    int i,cnt=0,t=0,flag=0;
    for(i=0;i<n;i++){
        cin>>a[i];
    }
    if (n==1) cnt=1;
    else{
    for(i=0;i<n;i++){
        t=a[i];
        if(a[i]==i)cnt++;
        else if(a[t]==i) flag=1;
    }
    if (flag==1) cnt=cnt+2;
    else cnt++;
    }
    cout <<cnt<<endl;
    return 0;
}

这个代码的具体意思就是说判断是不是有能交换的,交换后刚好满足有两个数再固定位置,+2或者+1

但是!
我忘记了要考虑不用交换的情况!!!
所以,思维的严密性非常重要

全部评论

相关推荐

02-22 20:28
重庆大学 Java
程序员牛肉:首先不要焦虑,你肯定是有希望的。 首先我觉得你得好好想一想自己想要什么。找不到开发岗就一定是失败的吗?那开发岗的35岁危机怎么说?因此无论是找工作还是考公我觉得你都需要慎重的想一想。但你一定要避开这样一个误区:“我是因为找不到工作所以不得不选择考公”。 千万不要这么想。你这个学历挺好的了,因此你投后端岗肯定是有面试机会的。有多少人简历写的再牛逼,直接连机筛简历都过不去有啥用?因此你先保持自信一点。 以你现在的水平的话,其实如果想要找到暑期实习就两个月:一个月做项目+深挖,并且不断的背八股。只要自己辛苦一点,五月份之前肯定是可以找到暑期实习的,你有点太过于高看大家之间的技术差距了。不要焦虑不要焦虑。 除此之外说回你这个简历内容的话,基本可以全丢了。如果想做后端,先踏踏实实做两个项目再说+背八股再说。如果想考公,那就直接备战考公。 但是但是就像我前面说的:你考公的理由可以是因为想追求稳定,想追求轻松。但唯独不能是因为觉得自己找不到工作。不能这么小瞧自己和自己的学历。
点赞 评论 收藏
分享
03-02 10:51
邵阳学院 Java
红鲤鱼与绿鲤鱼i:看了你的头像不像找工作,像在找妹子
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务