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的更多相关文章
随机推荐
- idea 2018.1 for mac JRebel破解
第一步: 在 Idea 中下载 Jrebel 路径:preferences-plugins-Browse repositories-直接搜索下载 Jrebel 第二步:配置反向代理工具 1.安装 ...
- tomcat的Jsp执行
Tomcat 会把为JSP页面创建的Servlet源文件和class类文件放置在“<TOMCAT_HOME>\work\Catalina\<主机名>\<应用程序名> ...
- ASP.NET Core MVC四种枚举绑定方式
前言 本节我们来讲讲在ASP.NET Core MVC又为我们提供了哪些方便,之前我们探讨过在ASP.NET MVC中下拉框绑定方式,这节我们来再来重点看看枚举绑定的方式,充分实现你所能想到的场景,满 ...
- 深入理解Java虚拟机(类文件结构+类加载机制+字节码执行引擎)
目录 1.类文件结构 1.1 Class类文件结构 1.2 魔数与Class文件的版本 1.3 常量池 1.4 访问标志 1.5 类索引.父索引与接口索引集合 1.6 字段表集合 1.7 方法集合 1 ...
- 基于 Token 的身份验证:JSON Web Token(附:Node.js 项目)
最近了解下基于 Token 的身份验证,跟大伙分享下.很多大型网站也都在用,比如 Facebook,Twitter,Google+,Github 等等,比起传统的身份验证方法,Token 扩展性更强, ...
- 小L记单词
题目描述 小L最近在努力学习英语,但是对一些词组总是记不住,小L小把这些词组中每一个单词的首字母都记一下,这样形成词组的缩写,通过这种方式小L的学习效率明显提高. 输入 输入有多行,每组测试数据占一行 ...
- java web石家庄铁道大学课程管理系统
package kecheng Kc.java package kecheng; public class Kc { private int id; private String classname; ...
- win64位安装python-mysqldb1.2.3
在其他版本的mysqldb里面时间查询有问题最后确定还是在 1.2.5 版本下来解决,需要解决的问题就是这个:“Cannot open include file: 'config-win.h': No ...
- ES7的新特性
ES7的新特性 ES7 特性: 1.Array.prototype.includes2.Exponentiation Operator(求幂运算) 一,Array.prototype.includes ...
- VS2015 + OPENCV + CUDA 安装流程
VS2015 https://blog.csdn.net/guxiaonuan/article/details/73775519?locationNum=2&fps=1 OPENCV htt ...