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的更多相关文章
随机推荐
- Linux并发与同步专题
并发访问:多个内核路径同时访问和操作数据,就有可能发生相互覆盖共享数据的情况,造成被访问数据的不一致. 临界区:访问和操作共享数据的代码段. 并发源:访问临界区的执行线程或代码路径. 在内核中产生并发 ...
- __attribute__ 机制详解(一)
GNU C 的一大特色就是__attribute__ 机制.__attribute__ 可以设置函数属性(Function Attribute).变量属性(Variable Attribute)和类型 ...
- 《Python神经网络编程》中文版PDF+英文版PDF+源代码,业界良心书
下载:https://pan.baidu.com/s/1hI6wMPq4UFvEmpgF3ZV1jg 关于内容 这本书主要揭示神经网络背后的概念,并介绍如何通过Python实现神经网络.全书主要讲了三 ...
- [Flume][Kafka]Flume 与 Kakfa结合例子(Kakfa 作为flume 的sink 输出到 Kafka topic)
Flume 与 Kakfa结合例子(Kakfa 作为flume 的sink 输出到 Kafka topic) 进行准备工作: $sudo mkdir -p /flume/web_spooldir$su ...
- Ajax跨越问题原因分析与解决思路
1.什么是AJAX跨域问题 简单来说,就是前端调用后端服务接口时 如果服务接口不是同一个域,就会产生跨域问题 2.AJAX跨域场景 前后端分离.服务化的开发模式 前后端开发独立,前端需要大量调用后端接 ...
- 高并发下的Id生成器
考虑到sql server以及c#,最多只能用decimal类型,也就是29位的数字,做了下面这个数字型id生成器: class Program { static void Main(string[] ...
- node express 静态资源
实例代码 const express = require('express') const path = require('path') const app = express() app.use(e ...
- .net core实践系列之短信服务-架构优化
前言 通过前面的几篇文章,讲解了一个短信服务的架构设计与实现.然而初始方案并非100%完美的,我们仍可以对该架构做一些优化与调整. 同时我也希望通过这篇文章与大家分享一下,我的架构设计理念. 源码地址 ...
- 【开源】小程序、小游戏和Web运动引擎 to2to 发布
简单轻量跨平台的 Javascript 运动引擎 Github → https://github.com/dntzhang/cax/tree/master/packages/to Simple DEM ...
- 海康威视笔试(C++)
1. select和epoll的区别 2.服务器并发量之高性能服务器设计 3.SQL关键字 4.TCP乱序和重传的问题 5.c++对象内存分配问题 6.c++多线程 join的用法: Thread类的 ...