ACM思维题训练集合
The Little Elephant has got a problem — somebody has been touching his sorted by non-decreasing array a of length n and possibly swapped some elements of the array.

The Little Elephant doesn't want to call the police until he understands if he could have accidentally changed the array himself. He thinks that he could have accidentally changed array a, only if array a can be sorted in no more than one operation of swapping elements (not necessarily adjacent). That is, the Little Elephant could have accidentally swapped some two elements.

Help the Little Elephant, determine if he could have accidentally changed the array a, sorted by non-decreasing, himself.

Input
The first line contains a single integer n (2 ≤ n ≤ 105) — the size of array a. The next line contains n positive integers, separated by single spaces and not exceeding 109, — array a.

Note that the elements of the array are not necessarily distinct numbers.

Output
In a single line print "YES" (without the quotes) if the Little Elephant could have accidentally changed the array himself, and "NO" (without the quotes) otherwise.
Examples
Input
2
1 2
Output
YES
Input
3
3 2 1
Output
YES
Input
4
4 3 2 1
Output
NO
Note
In the first sample the array has already been sorted, so to sort it, we need 0 swap operations, that is not more than 1. Thus, the answer is "YES".

In the second sample we can sort the array if we swap elements 1 and 3, so we need 1 swap operation to sort the array. Thus, the answer is "YES".

In the third sample we can't sort the array in more than one swap operation, so the answer is "NO".

题目中说原来的数列非递减,也就是非严格递增,显然原数列易得,排遍序即可,如果发生了一次交换至多有两个不一样。

#include <bits/stdc++.h>
using namespace std;
const int maxn=100055;
int a[maxn];
int b[maxn];
int main()
{int n;cin>>n;for(int i=0;i<n;i++){cin>>a[i];b[i]=a[i];}sort(b,b+n);int cnt=0;for(int i=0;i<n;i++){if(a[i]!=b[i]) cnt++;if(cnt==3) break;}if(cnt>2) cout<<"NO"<<endl;else cout<<"YES"<<endl;}

CF--思维练习--CodeForces - 221C-H - Little Elephant and Problem (思维)相关推荐

  1. Codeforces - Ehab and a component choosing problem

    题目链接:Codeforces - Ehab and a component choosing problem 显然 Σa / k,相当于取平均值,我们得到最大值肯定是k=1的时候. 求最大值树形dp ...

  2. Codeforces 798C:Mike and gcd problem

    Codeforces 798C:Mike and gcd problem 题目链接:http://codeforces.com/contest/798/problem/C 题目大意:给出一个大小为$n ...

  3. 项目思维导图软件测试分析,在软件测试中使用思维导图技术

    我们的大脑比书面文字更能处理和回忆视觉效果.视觉图像能够以描述性词语无法描述的方式带来快速的清晰度和理解.思维导图是利用这个事实来形象化地组织和呈现信息的工具. 思维导图软件测试 思维导图是一个想法或 ...

  4. 【底层思维】思维的深度决定人生的高度,底层思维的4个方法论

    爱因斯坦说,我们不能在制造问题的层级来解决问题. 所以,想要真正地解决难题就要学会升维思考,从而找到问题的底层逻辑. 而且思维越底层,思考的维度就越高. 听有道君这么说,你一定在想,我知道要升维思考, ...

  5. 思维导图(一):高效的思维工具

    从线性思考到非线性思考 人类的思维可以分为线性思维和非线性思维两种.一般来讲,线性思维是一种直线的.单向的.单维的.缺乏变化的思维方式,如逻辑思维:非线性思维则是相互连接的,非平面.立体化.无中心.无 ...

  6. 什么是思维导图?有哪些好用的思维导图工具

    思维导图又叫做心智图,是利用图形化的方式进行发散性思维的一种工具,它把复杂性的知识体系转化为形象化的图形表示,更加形象具体.思维导图利用图形化的方式充分调动左右脑的能力,开启我们大脑的潜能. 思维导图 ...

  7. github 思维导图开元软件_哪款思维导图软件比较好用?

    谢邀~ 思维导图作为一种风靡全球的知识梳理工具,广泛的运用于各个行业中,可以用来做头脑风暴.知识点梳理.项目管理等等.下面盘点了市面上几款主流好用的思维导图工具. 优点:脑图风格多样,适用于多场合,思 ...

  8. 思维导图在线制作,好用的思维导图软件分享

    在工作和学习中,我们经常会涉及到数据分析.罗列计划.工作或学习总结等等,这时候一款思维导图软件可以很好的帮助我们实现思维梳理,并激发无限创意和灵感. 目前市面上有很多思维导图软件,像XMind是大家比 ...

  9. 计算机思维典型方法有抽象,传说中的四大编程思维 一篇彻底搞清楚

    对于很多家长朋友来说,编程还是一个比较模糊的概念. 比如觉得编程就是敲代码.程序员的工作.一门兴趣课......如果要问,什么是编程思维,这大概更让大家觉得脑瓜疼. 小童希望这篇文章能帮大家对编程有更 ...

最新文章

  1. Android界面开发问题总结
  2. 降噪电路_拆解报告:Mobvoi出门问问 Ticpods ANC 真无线降噪耳机
  3. html在百度地图显示位置,在百度地图显示当前位置.html
  4. 使用 .NET Core 中的 EventCounters 衡量性能
  5. 傻子都能看懂的马拉车Manacher
  6. ubuntu18 安装完怎么设置中文输入法?
  7. MooTools官方插件 - Chain.Wait
  8. 图像处理自学(五):CAMERA驱动软件硬件架构V4L2
  9. 看看你的密码有多安全?
  10. 02 Oracle 批量导出建表语句和数据
  11. The Luckiest Number 欧拉函数
  12. 一款极致的文件对比工具——Beyond Compare,适用于Windows、macOS和Linux的文件和文件夹比较
  13. LeaRun快速开发平台,.Net Core加持,功能更全面
  14. 朋友去面试阿里蚂蚁金服测试岗位过程经历
  15. halcon第七讲:基于训练学习的颜色检测
  16. SSM框架之数据分页,模糊查询
  17. Java编写三角形和平行四边形
  18. 微信小程序图片实现宽度100%,高度自适应
  19. 如何测试WizFi210的”串口转无线”功能?
  20. openFrameworks实现的简单坦克对战游戏-Tank War

热门文章

  1. JAVA子类和父类在同一个包中,子类和父类在同一个包中继承性
  2. Java数组参考_Java数组
  3. 性能优化(数据库设计原则)
  4. 关于分布式计算的一些概念
  5. 在SuSE10.1上安装Oracle10g
  6. IO概述、异常、File文件类_DAY19
  7. hdu2019——数列有序解题报告
  8. python getopt
  9. Debian Squeeze AMD64安装Oracle 10g x86_64 10.2.0....
  10. ASP.Net中怎样获得存储过程传出的参数。