题意:给定 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. Linux USB 摄像头驱动

    在 cortex-a8 中,可接入摄像头的接口通常可以分为两种, CAMERA 接口和 USB 接口的摄像头.这一章主要是介绍 USB 摄像头的设备驱动程序.在我们印象中,驱动程序都是一个萝卜一个坑, ...

  2. java实现心型、99乘法demo

    package com.js.ai.modules.pointwall.interfac; import java.awt.Font; import javax.print.attribute.sta ...

  3. Oracle 10 Recycle Bin回收站

    这个功能从10g开始有了. (1)什么是Recycle Bin实际上,Recycle Bin只是一个保存被drop的对象的一个数据字典表.所以,可以通过如下语句查询回收站中的信息:select * f ...

  4. oracle打补丁步骤简介

    1.了解opatchopatch是用于维护"个别"补丁的,有人称其为interim path或是one-off patch该命令的存放位置在$ORACLE_HOME下的OPatch ...

  5. python dns欺骗

    '''' from scapy.all import * from threading import Thread def DNShijacking(): global wg wg=raw_input ...

  6. Tkinter place() 方法

    Python GUI - Tkinter place() 方法的使用例子:   这个的几何管理器组织放置在一个特定的位置,在他们的父widget部件. 语法: widget.place( place_ ...

  7. mongodb主从复制 副本集(六)

    主从复制副本集 8888.conf dbpath = D:\software\MongoDBDATA\07\8888 #主数据库地址port = 8888 #主数据库端口号bind_ip = 127. ...

  8. grideh SelectedRows Bookmark

    VCL grideh 选中多行 TBookmark.Bookmark.GotoBookmark TBookmark bm= DataSet->GetBookmark(); DataSet-> ...

  9. 自动删除 Elasticsearch 索引

    #!/bin/bash # author: Wang XiaoQiang# crontab -e# 0 0 * * * /root/script/del_esindex.sh # auto delet ...

  10. MySQL批量添加表字段

    ALTER TABLE custom ADD contacts2 VARCHAR(50) NOT NULL DEFAULT '' COMMENT '客户联系人2',ADD phone2 VARCHAR ...