[Codeforces] 347B - Fixed Points
题意:给定一个序列,现有一种操作:两个数的位置互换。问最多操作一次。序列 [元素位置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的更多相关文章
- CodeForces 347B Fixed Points (水题)
题意:给定 n 数,让你交换最多1次,求满足 ai = i的元素个数. 析:很简单么,只要暴力一遍就OK了,先把符合的扫出来,然后再想,最多只能交换一次,也就是说最多也就是加两个,然后一个的判,注意数 ...
- codeforces B.Fixed Points
link:http://codeforces.com/contest/347/problem/B 很简单,最多只能交换一次,也就是说,最多会增加两个.可能会增加一个.也可能一个也不增加(此时都是fix ...
- codeforces B. Fixed Points 解题报告
题目链接:http://codeforces.com/problemset/problem/347/B 题目意思:给出一个包含n个数的排列a,在排列a中最多只能作一次交换,使得ai = i 这样的匹配 ...
- B. Fixed Points
B. Fixed Points time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- codeforces 19D D. Points 树套树
D. Points Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/19/problem/D De ...
- codeforces 577E E. Points on Plane(构造+分块)
题目链接: E. Points on Plane time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- cf B. Fixed Points
http://codeforces.com/contest/347/problem/B #include <cstdio> #include <cstring> #includ ...
- codeforces 19 D. Points(线段树+set二分)
题目链接:http://codeforces.com/contest/19/problem/D 题意:给出3种操作:1)添加点(x,y),2)删除点(x,y),3)查询离(x,y)最近的右上方的点. ...
- Codeforces C Match Points(二分贪心)
题目描述: Match Points time limit per test 2 seconds memory limit per test 256 mega bytes input standard ...
随机推荐
- ios 照片编辑的view封装
转:http://www.cnblogs.com/xiaobaizhu/archive/2013/07/03/3170101.html 该控件有旋转,缩放,拖动,剪裁的功能,封装成了一个ImageCr ...
- 软件测试技术(三)——使用因果图法进行的UI测试
目标程序 较上次增加两个相同的输入框 使用方法介绍 因果图法 在Introduction to Software Testing by Paul一书中,将软件测试的覆盖标准划分为四类,logical ...
- C ~ char int 等数据转换问题
1,char型数字转换为int型 "; printf(]-');//输出结果为3 2,int转化为char (1)字符串转换成数字,用atoi,atol,atof,分别对应的是整型,long ...
- Python 实例: 备份文件
都说生命苦短,我用python, 所以这两天我也开始学python了. 昨天搞了下语法,今天搞出来个实例,备份文件.尽管编码相当烂,但是测试了一下,还真能用. 它读取一个任务文件, 根据指定的任务参数 ...
- CentsOS7 网络自动启动
虚拟机中安装完成CentOS7后,网络总是需要手工启动才可使用,设置为自动连接的方式如下: cd /etc/sysconfig/network-scripts/ls #找到类似的文件:ifcfg-et ...
- python学习之subprocess模块
subprocess.Popen 这个模块主要就提供一个类Popen: class subprocess.Popen( args, bufsize=0, executable=None, stdin= ...
- mac搭建PHP开发环境
在Mac系统上搭建Php服务器环境: LAMP: Linux Apache MySQL PHP MAMP: MACOS APACHE(自带) MYSQL(需自己安装) PHP(自带) 一.APACHE ...
- windwos iis 7.5 使用html 报405错误
今天遇到了这个问题,网上搜一下基本上都是下面的答案: <form> 没有指定action的话就是文件自身了. .html本身是不可执行的,如果要修改的话,在IIS中站点属性- 主目录 - ...
- Gradle – Spring 4 MVC Hello World Example – Annotation
In this tutorial, we will take the previous Gradle + Spring MVC XML example, rewrite it to support @ ...
- 【Java】IO技术的使用——用IO实现大文件的复制
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827481.html 用IO进行文件复制,实质就是用FileInputStream链接要复制的文件,按一定规模 ...