CF220A.Little Elephant and Problem
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
小象遇到了一个问题——有人动了他的长度为 n 的非递减排序数组 a,并可能交换了数组中的某些元素。
在弄清楚自己是否可能不小心改动了该数组之前,小象不想报警。他认为,仅当数组 a 可以通过至多一次元素交换操作(不一定是相邻元素)被重新排序为非递减序列时,才可能是他自己不小心改动了该数组。也就是说,小象最多只可能不小心交换了其中的两个元素。
请帮助小象判断:这个原本是非递减排序的数组 a,是否有可能是他自己不小心改动的?
输入格式
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.
第一行包含一个整数 n(2≤n≤105)——数组 a 的大小。
下一行包含 n 个正整数,以单个空格分隔,且每个数不超过 109 —— 即数组 a。
注意:数组中的元素不一定是互不相同的数。
输出格式
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.
如果小象有可能自己不小心修改了该数组,则在一行中输出 "YES"(不带引号);否则输出 "NO"(不带引号)。
输入输出样例
输入#1
2 1 2
输出#1
YES
输入#2
3 3 2 1
输出#2
YES
输入#3
4 4 3 2 1
输出#3
NO
说明/提示
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".
在第一个样例中,数组已经排好序,因此要将其排序需要 0 次交换操作,不超过 1 次。因此,答案为 "YES"。
在第二个样例中,我们可以通过交换元素 1 和 3 来对数组排序,因此需要 1 次交换操作来完成排序。因此,答案为 "YES"。
在第三个样例中,我们无法在至多一次交换操作内将数组排序,因此答案为 "NO"。
输入解题思路,AI测评打分。不知道怎么写?