[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 ...
随机推荐
- Ch04-文字列表的设计
Ch04: 文字列表的设计 4.1 编号列表 语法: <OL> <li>编号1 <li>编号2 ...... </OL> 属性: 编号类型:type=1 ...
- HDU 5489 Removed Interval
题意:求一段序列中删掉L个连续元素后的LIS. 解法:我的想法很复杂= =怎么说呢……首先用nlogn的方法求LIS得到的序列dp的第i项的意义为上升子序列所有长度为i的序列结尾元素的最小值,那么先倒 ...
- C# 委托总结
总结 委托的本质: 委托是一种特殊的数据类型,它表示某种特定类型的函数,并且可以表示多个函数,将这些函数串联起来.使用委托就好像函数调用一样. 委托实质上是一个类,编译器会根据关键字delegate自 ...
- duilib corner属性的贴图技巧——让图片自动贴到控件的的某一边或者一角并自适应控件的大小
转载请说明原出处,谢谢~~ Duilib给控件贴图功能可以附带多个属性,各个属性的配合可以达到许多效果.以下是duilib支持的所有贴图属性: 贴图描述: Duilib的表现力丰富很大程度上得益于贴图 ...
- 【转载】epoll的使用
select,poll,epoll简介 select select本质上是通过设置或者检查存放fd标志位的数据结构来进行下一步处理.这样所带来的缺点是: 1 单个进程可监视的fd数量被限制 2 需要维 ...
- Python面向对象2
方法 公共方法 私有方法 类方法 静态方法 #!usr/bin/python #coding:utf8 class Milo(): name = 'csvt' def fun1(self): prin ...
- hbase使用-java操作
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courie ...
- CentOS 7 最小化安装之后安装Mysql
启动网卡 验证网络管理器和网卡接口状态 # systemctl status NetworkManager.service # nmcli dev status 修改网卡配置文件 通过上一步可以看到有 ...
- 制作java可执行程序的方法
命令行方法: 1. 创建Manifest.txt文件,内容: Main-Class: com.mkyong.awt.AwtExample 2.打包所有的class,包括Manifest.txt文件: ...
- Hibernate之管理session与批处理
1. Hibernate 自身提供了三种管理Session对象的方法 –Session对象的生命周期与本地线程绑定 –Session 对象的生命周期与JTA事务绑定 –Hibernate 委托程序管理 ...