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的更多相关文章
随机推荐
- HotSpot虚拟机对象探秘(对象创建,对象内存布局,对象访问定位)
以常用的HotSpot虚拟机和JAVA内存区域堆为例,探讨对象的创建,对象的内存布局以及对象的访问定位 一.对象的创建 1)类加载:虚拟机遇到一条new指令时,先检测这个指令的参数能否在常量池中定位到 ...
- 【C#复习总结】垃圾回收机制(GC)1
摘要:今天我们漫谈C#中的垃圾回收机制,本文将从垃圾回收机制的原理讲起,希望对大家有所帮助. GC的前世与今生 虽然本文是以.NET作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年,由 ...
- Bootstrap 栅格 样式 组件 插件
-----------------------------起先是我们造成习惯,后来是习惯造成我们. day 51 Bootstrap 官方网站: bootcss.com/ <!DOCTYP ...
- VMware vSphere 6 序列号
vSphere 6 Hypervisor HY0XH-D508H-081U8-JA2GH-CCUM2 4C4WK-8KH8L-H85J0-UHCNK-8CKQ8 NV09R-2W007-08D38-C ...
- Django的路由层
U RL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于客户端发来的某个URL调用哪一段逻辑代 ...
- webpack之loader和plugin简介
webpack之loader和plugin简介 webpack入门和实战(二):全面理解和运用loader和plugins webpack入门(四)——webpack loader 和plugin w ...
- 【转】Linux下cp: omitting directory `XXX'问题解决
在linux系统中复制文件夹时提示如下: Shell代码 [root@idtp4 site-packages]# /site-packages/ cp: omitting directory ‘yag ...
- openstack,docker,mesos,k8s关系
openstack,docker,mesos,k8s什么关系? - 知乎https://www.zhihu.com/question/62985699 OpenStack + K8S 环境集成测试ht ...
- zabbix使用jmx监控tomcat
zabbix监控Tomcat/JVM实例性能(115) – 运维生存时间http://www.ttlsa.com/zabbix/zabbix-use-jmx-monitor-tomcat/ zabbi ...
- Jmeter使用笔记之html报告扩展(一)
题记:在用loadrunner的时候可以生成一个HTML的报告,并且里面包含各种图表,各种详细的数据.而在使用Jmeter测试完后并不能直接生成Html 的报告(无论是用GUI还是命令行启动). 经过 ...