codefroces Round #201.B--Fixed Points
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.
代码:
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+3;
int vis[MAXN];
int main()
{
memset(vis,-1,sizeof(vis));
int n,x;
cin>>n;
int res=0;
for(int i=0;i<n;i++){
cin>>x;
if(x!=i) vis[i]=x;
else res++;
}
//cout<<res<<endl;
int flag=0;
for(int i=0;i<n;i++){
if(vis[i]!=-1){
if(vis[vis[i]]!=-1){
flag=1;
}
if(vis[vis[i]]==i){
flag=2;
break;
}
}
}
//cout<<flag<<endl;
// for(int i=0;i<n;i++){
// cout<<vis[i]<<"\t";
// }
//cout<<endl;
cout<<res+flag<<endl;
}
codefroces Round #201.B--Fixed Points的更多相关文章
- codefroces Round #201.a--Difference Row
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description You wa ...
- B. Fixed Points
B. Fixed Points time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- codeforces B.Fixed Points
link:http://codeforces.com/contest/347/problem/B 很简单,最多只能交换一次,也就是说,最多会增加两个.可能会增加一个.也可能一个也不增加(此时都是fix ...
- Educational Codeforces Round 46 C - Covered Points Count
C - Covered Points Count emmm 好像是先离散化一下 注意 R需要+1 这样可以确定端点 emmm 扫描线?瞎搞一下? #include<bits/stdc++.h&g ...
- Educational Codeforces Round 64 C. Match Points 【二分思想】
一 题面 C. Match Points 二 分析 根据题意很容易想到要去找满足条件的数,因为可以打乱输入的顺序,所以很容易想到二分. 但是如果直接对输入的数组进行二分,如输入$a$,直接在数组里二分 ...
- codeforces B. Fixed Points 解题报告
题目链接:http://codeforces.com/problemset/problem/347/B 题目意思:给出一个包含n个数的排列a,在排列a中最多只能作一次交换,使得ai = i 这样的匹配 ...
- codeforces round #201 Div2 A. Difference Row
#include <iostream> #include <vector> #include <algorithm> using namespace std; in ...
- codeforce Codeforces Round #201 (Div. 2)
cf 上的一道好题: 首先发现能生成所有数字-N 判断奇偶 就行了,但想不出来,如何生成所有数字,解题报告 说是 所有数字的中最大的那个数/所有数字的最小公倍数,好像有道理:纪念纪念: #incl ...
- Codeforces Round #201 (Div. 2) - C. Alice and Bob
题目链接:http://codeforces.com/contest/347/problem/C 题意是给你一个数n,然后n个数,这些数互不相同.每次可以取两个数x和y,然后可以得到|x - y|这个 ...
随机推荐
- # IDEA相关知识
目录 IDEA相关知识 安装目录下: 配置目录下: 工程目录下: 名词解释 IDEA相关知识 安装目录下: bin:启动文件,配置信息,IDEA的一些属性信息 jre64:IDEA自带的运行环境 li ...
- GitHub从小白到熟悉<一>
注册开始
- win10操作系统的安装
电脑被重装操作系统了,一切从头开始啦!!! 不过倒是学习了,给大家分享一些学习经验~ 1:制作启动盘 制作启动盘的首先要准备一个空的U盘,为什么说空的呢,因为制作的时候会格式化U盘,只能存个操作系统, ...
- Asp.net4.5未在web服务器上注册
在使用vs2012打开项目时,显示Asp.net4.5未在web服务器上注册?是由于没有下载一个补丁的原因,只需下载安装补丁 VS11-KB3002339.exe 下载地址:https://downl ...
- c# 简单打开关闭摄像头
const short WM_CAP = 1024; const int WM_CAP_DRIVER_CONNECT = WM_CAP + 10; const int WM_CAP_DRIVER_DI ...
- 02Spring基于xml的IOC配置--实例化Bean的三种方式
maven依赖 <dependencies> <!--IOC相关依赖--> <dependency> <groupId>org.springframew ...
- ArcGis之popup列表字段自定义
ArcGis之popup列表字段自定义 featureLayer图层中可以使用popupTemplate属性添加弹窗. API:https://developers.arcgis.com/javasc ...
- 无线传输模块HC-12
无线传输模块HC-12使用 因为实验室的无人机需要使用一款无线传输模块进行遥控控制,我们讨论的中测试了HC-12,并对HC-12传输距离进行了简单测试.在此做下使用记录. 模块概述 HC-12 无线串 ...
- python爬去虎扑数据信息,完成可视化
首先分析虎扑页面数据 如图我们所有需要的数据都在其中所以我们获取需要的内容直接利用beaitifulsoupui4``` soup.find_all('a',class_ ...
- jq自动触发事件
$('.btn_fath ').trigger("click");$('.btn_fath ').click();