[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 ...
随机推荐
- PIG的配置
Pig是一个客户端应用程序,就算你要在Hadoop集群上运行Pig,也不需要在集群上装额外的东西.Pig的配置非常简单: 1.下载pig,网址http://pig.apache.org/ 2.在机器上 ...
- 1047图的深度优先遍历c语言
描述 图(graph)是数据结构 G=(V,E),其中V是G中结点的有限非空集合,结点的偶对称为边(edge):E是G中边的有限集合.设V={0,1,2,……,n-1},图中的结点又称为顶点(vert ...
- Dapper.net 在Parameterized时对于String的扩展(转)
虽然Dapper通过提供的DbString本身支持对于String的指定Parameterized,但这方法明显不够,当Insert时,我们更希望是把一个Poco直接传递过去,而不是来new一个匿名函 ...
- Matlab编程实例(1) 移动平均
MATLAB数字信号处理作业,把自己写的程序发上来..欢迎交流~ QQ 五幺九七九零六四 首先是任意点移动平均: 主程序:mov_average_main.m (运行) 函数:mov_averag ...
- 【LeetCode 173】Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- String.Format格式说明
原文地址:http://www.cnblogs.com/tuyile006/archive/2006/07/13/449884.aspx C#格式化数值结果表 字符 说明 示例 输出 C 货币 str ...
- SQL你必须知道的-查询聚合分组排序
use MySchoolTwo -- 简单查询 select * from Student -- 话说这种查询的效率要比 * 要高级点 select sId , sName , ...
- C++程序设计之结构体,共用体,枚举和typedef
[1]结构体的基本功 注意结构体里面可以有很多东西,可以结构体里面包含结构体 #include<iostream> using namespace std; struct Date { i ...
- ndk文件操作问题及小结
最近在做文件传输,发现在android下用f系列的C库函数去读取文件文件大小会受到2G大小的约束,查阅了很久,最后只能去看google的libc源码,发现了以下几个问题: 1.bionic的libc是 ...
- Codeforces Round #363 (Div. 1) C. LRU
题意: n个数,长度为k的缓存,每次询问,每个数以pi的概率被选,如果不在缓存区则加入,如果缓存区满了,则第一个进缓存的出来,问10^100次询问以后每个数在缓存的概率 思路: 状压DP,看了hzwe ...