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

依据题意,最多个数为 :

【操作之前[元素位置i]  与 [元素Ai] 相等的个数】 +  【调换两个 [元素位置i]  与 [元素Ai] 不相等 的元素 使某个 [元素位置i]  与 [元素Ai] 相等的个数】。

举个样例:

0 1 2 4 3

这个序列,调换后面两个元素,正好使每一个 [元素位置i]  与 [元素Ai] 相等

也就是说,调换的时候,不用考虑 [元素位置i]  与 [元素Ai] 相等 的元素了。

直接考虑 [元素位置i]  与 [元素Ai] 不相等 的元素中,是否能调换一次之后使 一个 或 两个元素[元素位置i]  与 [元素Ai] 相等

调换后若有1个元素 [元素位置i]  与 [元素Ai] 相等

那么最多个数 = 【操作之前[元素位置i]  与 [元素Ai] 相等的个数】 + 1,若有2个元素则 + 2。

接下来就能够依据这个用map来解决问题了。

#include <stdio.h>
#include <map>
using namespace std;
int main()
{
int n, ans;
while(~scanf("%d", &n))
{
map<int, int> mm;
ans = 0;
for(int i = 0; i < n; i++)
{
int temp;
scanf("%d", &temp);
if(temp == i)
{
++ans;
}
else
{
mm[i] = temp;
}
}
map<int, int>::iterator it = mm.begin();
for(;it != mm.end();it++)
{
if(mm.find((*it).second) != mm.end() && mm[(*it).second] == (*it).first)
{
++ans;
break;
}
}
if(ans != n)
++ans;
printf("%d\n", ans);
}
return 0;
}

[Codeforces] 347B - Fixed Points的更多相关文章

  1. CodeForces 347B Fixed Points (水题)

    题意:给定 n 数,让你交换最多1次,求满足 ai = i的元素个数. 析:很简单么,只要暴力一遍就OK了,先把符合的扫出来,然后再想,最多只能交换一次,也就是说最多也就是加两个,然后一个的判,注意数 ...

  2. codeforces B.Fixed Points

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

  3. codeforces B. Fixed Points 解题报告

    题目链接:http://codeforces.com/problemset/problem/347/B 题目意思:给出一个包含n个数的排列a,在排列a中最多只能作一次交换,使得ai = i 这样的匹配 ...

  4. B. Fixed Points

    B. Fixed Points time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  5. codeforces 19D D. Points 树套树

    D. Points Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/19/problem/D De ...

  6. codeforces 577E E. Points on Plane(构造+分块)

    题目链接: E. Points on Plane time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  7. cf B. Fixed Points

    http://codeforces.com/contest/347/problem/B #include <cstdio> #include <cstring> #includ ...

  8. codeforces 19 D. Points(线段树+set二分)

    题目链接:http://codeforces.com/contest/19/problem/D 题意:给出3种操作:1)添加点(x,y),2)删除点(x,y),3)查询离(x,y)最近的右上方的点. ...

  9. Codeforces C Match Points(二分贪心)

    题目描述: Match Points time limit per test 2 seconds memory limit per test 256 mega bytes input standard ...

随机推荐

  1. ioctl()获取本地网卡设备信息

    获得eth0接口所有信息: #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #inclu ...

  2. Android中不混淆类中函数

    情况一:混淆不同的函数aTest.bTest -keep class com.zony.Test { void aTest(byte[], int, int); void bTest(String, ...

  3. Delphi TRichEdit加载word内容

    procedure TForm1.btn6Click(Sender: TObject);var WordApp: Variant; //声明一个word对象beginWordApp := Create ...

  4. Maximum Random Walk(概率dp)

    题意: 走n步,给出每步向左走概率l,向右走概率r,留在原地的概率 1-l-r,求能达到的最远右边距离的期望. 分析: 开始按期望逆求的方式分析,但让求的就是右边界没法退,懵了一会,既然逆着不能求,就 ...

  5. iOS已发布应用中对异常信息捕获和处理

    iOS已发布应用中对异常信息捕获和处理 iOS开发中我们会遇到程序抛出异常退出的情况,如果是在调试的过程中,异常的信息是一目了然,但是如果是在已经发布的程序中,获取异常的信息有时候是比较困难的. iO ...

  6. angular form-data文件上传

    前言:很久没更新博客,最近公司pc端技术选型用angular,这几天就赶鸭子上架,硬着头皮直接上手angular.其中有许多小坑陆陆续续踩起走.今天就遇到一个比较常见的问题:图片上传. 主题:图片上传 ...

  7. codeforce 702E Analysis of Pathes in Functional Graph RMQ+二进制

    http://codeforces.com/contest/702 题意:n个点,n条边,每个点出边只有一条,问从每个点出发经过k条边的边权和,以及边权最小值 思路: f[i][j] 第i个点出发,经 ...

  8. Java中Runnable和Thread的区别(转)

    http://developer.51cto.com/art/201203/321042.htm 第一种方式:使用Runnable接口创建线程 第二种方式:直接继承Thread类创建对象 使用Runn ...

  9. MySQL数据库备份和还原

    备份MySQL数据库的命令 mysqldump -hhostname -uusername -ppassword databasename > backupfile.sql 备份MySQL数据库 ...

  10. commons-lang3-3.4.jar

    StringUtils 1.StringUtils.isBlank(str); 检查字符串是否为空白(“ ”),为空(“”),为null. * StringUtils.isBlank(null)    ...