codeforces347B
Fixed Points
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 nintegers 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
5
0 1 3 4 2
3 sol:容易发现交换最多使得答案增加2,一般都能加1(似乎并没什么用)
于是O(n)扫一遍能加2就加2,否则试试能不能加1
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,a[N],Pos[N];
int main()
{
int i,ans=;
R(n);
for(i=;i<=n;i++)
{
a[i]=(read()+); Pos[a[i]]=i;
if(a[i]==i) ans++;
}
for(i=;i<=n;i++) if(a[i]!=i)
{
if(Pos[Pos[a[i]]]==a[i])
{
Wl(ans+);
return ;
}
}
Wl(min(ans+,n));
return ;
}
/*
input
5
0 1 3 4 2
output
3
*/
codeforces347B的更多相关文章
随机推荐
- 负载(Load)分析及问题排查
平常的工作中,在衡量服务器的性能时,经常会涉及到几个指标,load.cpu.mem.qps.rt等.每个指标都有其独特的意义,很多时候在线上出现问题时,往往会伴随着某些指标的异常.大部分情况下,在问题 ...
- keras神经网络三个例子
keras构造神经网络,非常之方便!以后就它了.本文给出了三个例子,都是普通的神经网络 例一.离散输出,单标签.多分类 例二.图像识别,单标签.多分类.没有用到卷积神经网络(CNN) 例三.时序预测, ...
- WPF效果(GIS三维篇)
二维的GIS已经被我玩烂了,紧接着就是三维了,哈哈!先来看看最简单的效果:
- 使用 IIS 在 Windows 上托管 ASP.NET Core2.0
准备: 操作系统:Windows Server 2008 R2 或更高版本 开发环境:VS2017 第一步:新建项目ASP.NET Core Web应用程序 在 Visual Studio 中,选择“ ...
- P2P平台介绍
https://www.ludou.org/tutengdai.html https://www.tutengdai.com/register?invite_code=9991300
- 修改docker0默认IP地址
第一步:vim /etc/docker/daemon.json { "registry-mirrors": ["https://docker.mirrors.ustc.e ...
- IP判断
题目描述 在基于Internet的程序中,我们常常需要判断一个IP字符串的合法性. 合法的IP是这样的形式: A.B.C.D 其中A.B.C.D均为位于[0, 255]中的整数.为了简单起见,我们规定 ...
- struts2之配置文件struts.xml详解
struts配置文件 struts.xml配置参数详解 struts.xml中很大一部分配置默认配置就好了 但是有些还是需要做了解 以便于理解 和修改 <?xml version=" ...
- POJ - 3468 线段树区间修改,区间求和
由于是区间求和,因此我们在更新某个节点的时候,需要往上更新节点信息,也就有了tree[root].val=tree[L(root)].val+tree[R(root)].val; 但是我们为了把懒标记 ...
- hdu5943素数间隙与二分匹配
题意: 给出n和s,匹配(s+1,s+2,s+3......s+n)和(1,2,3,4,5........n)让(s+x)%x==0,判断是否有解 思路: 先用程序跑一边,发现1到1e9得素数间隙小于 ...