题意:给定 n 数,让你交换最多1次,求满足 ai = i的元素个数。

析:很简单么,只要暴力一遍就OK了,先把符合的扫出来,然后再想,最多只能交换一次,也就是说最多也就是加两个,然后一个的判,注意数组越界。

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring> using namespace std;
const int maxn = 1e5 + 5;
int a[maxn]; int main(){
int n;
cin >> n;
int ans = 0;
for(int i = 0; i < n; ++i){
cin >> a[i];
if(a[i] == i) ++ans;
}
int cnt = 0;
for(int i = 0; i < n; ++i){
if(a[i] != i){
if(a[i] < n && a[a[i]] == i){ cnt = 2; break; }
else if(a[i] < n) cnt = 1;
}
}
cout << ans + cnt << endl;
return 0;
}

CodeForces 347B Fixed Points (水题)的更多相关文章

  1. [Codeforces] 347B - Fixed Points

    题意:给定一个序列,现有一种操作:两个数的位置互换.问最多操作一次.序列 [元素位置i]  与 [元素Ai] 相等的最多个数? 依据题意,最多个数为 : [操作之前[元素位置i]  与 [元素Ai] ...

  2. Codeforces Gym 100531G Grave 水题

    Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...

  3. codeforces 706A A. Beru-taxi(水题)

    题目链接: A. Beru-taxi 题意: 问那个taxi到他的时间最短,水题; AC代码: #include <iostream> #include <cstdio> #i ...

  4. codeforces 569B B. Inventory(水题)

    题目链接: B. Inventory time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. Codeforces 489A SwapSort (水题)

    A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  6. codeforces 688A A. Opponents(水题)

    题目链接: A. Opponents time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. Codeforces Round #594 (Div. 2) A. Integer Points 水题

    A. Integer Points DLS and JLS are bored with a Math lesson. In order to entertain themselves, DLS to ...

  8. codeforces B.Fixed Points

    link:http://codeforces.com/contest/347/problem/B 很简单,最多只能交换一次,也就是说,最多会增加两个.可能会增加一个.也可能一个也不增加(此时都是fix ...

  9. CodeForces 534B Covered Path (水题)

    题意:给定两个速度,一个一初速度,一个末速度,然后给定 t 秒时间,还每秒速度最多变化多少,让你求最长距离. 析:其实这个题很水的,看一遍就知道怎么做了,很明显就是先从末速度开始算起,然后倒着推. 代 ...

随机推荐

  1. 十七 能停止的线程 暴力停止 和 interrupt/return方法

    1:stop: 使用stop() 停止的线程则是非常暴力的. stop() 已经废弃了,因为: 1 如果强制停止则有可能使得一些清理工作得不到完成. 2 对锁定的对象进行了“解锁”,导致数据得不到同步 ...

  2. buffer cache 深度解析

    本文首先详细介绍了oracle中buffer cache的概念以及所包含的内存结构.然后结合各个后台进程(包括DBWRn.CKPT.LGWR等)深入介绍了oracle对于buffer cache的管理 ...

  3. Netty入门二:开发第一个Netty应用程序

    Netty入门二:开发第一个Netty应用程序 时间 2014-05-07 18:25:43  CSDN博客 原文  http://blog.csdn.net/suifeng3051/article/ ...

  4. OpenLayers 3 之 切换图层控件

    OpenLayers 3 之 切换图层控件 openlayers 3中并没有默认的图层切换控件,GitHub中有一个项目实现了我们需要的控件-------- ol3-layerswitcher . 可 ...

  5. vue引入jQuery、bootstrap

    vue引入jQuery.bootstrap 1.使用vue-cli构建的vue项目 2.npm安装jquery.bootstrap npm install jquery 3.修改build/webpa ...

  6. 全景之HDR

    全景之HDR拍摄 全景HDR流程: 1.相机拍摄 1.1  HDR拍摄 HDR拍摄需要拍摄不同曝光度的多张图片. 1.2 摄像师消去 需要摄像师在不同位置(一般为相机的两面),拍摄两次HDR. 注意: ...

  7. leetcode260

    public class Solution { public int[] SingleNumber(int[] nums) { var dic = new Dictionary<int, int ...

  8. css style study

    <img style="float:none;display:block;margin:0px auto" src="images/aa.jpg"> ...

  9. python算法之归并排序

    归并排序 归并排序是采用分治法的一个非常典型的应用.归并排序的思想就是先递归分解数组,再合并数组. 将数组分解最小之后,然后合并两个有序数组,基本思路是比较两个数组的最前面的数,谁小就先取谁,取了后相 ...

  10. JS倒计时,自动提交表单!

    <form id="frm" action="http://www.baidu.com"> 考试还剩余<div id="time&q ...