CF--思维练习--CodeForces - 221C-H - Little Elephant and Problem (思维)
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;
}
- Shell系列-Shell概述
- sqoop2系统入门之2汇总:用户指南shell命令
- 深入解析golang编程中函数的用法
- MySQL数据库(一):安装MySQL数据库
- Spring Security入门(二):基于数据库验证
- flume搜集日志:如何解决实时不断追加的日志文件及不断增加的文件个数问题
- hduoj-----(2896)病毒侵袭(ac自动机)
- MySQL数据库(二):基本管理
- Golang编程实现生成n个从a到b不重复随机数的方法
- TiDB 在株式会社 FUNYOURS JAPAN 的应用
- MySQL数据库(三):数据类型
- spark开发环境详细教程2:window下sbt库的设置
- hdu----(2222)Keywords Search(ac自动机)
- MySQL数据库(四):约束条件
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- django-URL之include函数(五)
- springmvc之使用ModelAttribute避免不允许被修改的值更新时为空
- 【colab pytorch】使用tensorboard可视化
- django-URL别名的作用(六)
- springmvc之如何确定目标方法Pojo类型的参数?
- 【colab pytorch】使用tensorboardcolab可视化
- 实用,完整的HTTP cookie指南
- django-URL之从URL中获取关键字(七)
- springmvc之使用POJO作为参数
- 【猫狗数据集】利用tensorboard可视化训练和测试过程
- springmvc之视图解析流程
- 【猫狗数据集】从命令行接收参数
- django-URL重定向(八)
- Jetpack新成员,App Startup一篇就懂
- python之利用魔术方法实现自己定义的二维向量