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 221 C. Little Elephant and Problem

    C. Little Elephant and Problem time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  2. Codeforces 221d D. Little Elephant and Array

    二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...

  3. Educational Codeforces Round 40 F. Runner's Problem

    Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...

  4. 2019~2020icpc亚洲区域赛徐州站H. Yuuki and a problem

    2019~2020icpc亚洲区域赛徐州站H. Yuuki and a problem 题意: 给定一个长度为\(n\)的序列,有两种操作: 1:单点修改. 2:查询区间\([L,R]\)范围内所有子 ...

  5. CF思维联系--CodeForces - 218C E - Ice Skating (并查集)

    题目地址:24道CF的DIv2 CD题有兴趣可以做一下. ACM思维题训练集合 Bajtek is learning to skate on ice. He's a beginner, so his ...

  6. CF思维联系– CodeForces - 991C Candies(二分)

    ACM思维题训练集合 After passing a test, Vasya got himself a box of n candies. He decided to eat an equal am ...

  7. CF思维联系–CodeForces - 225C. Barcode(二路动态规划)

    ACM思维题训练集合 Desciption You've got an n × m pixel picture. Each pixel can be white or black. Your task ...

  8. CF思维联系–CodeForces -224C - Bracket Sequence

    ACM思维题训练集合 A bracket sequence is a string, containing only characters "(", ")", ...

  9. CF思维联系–CodeForces - 223 C Partial Sums(组合数学的先线性递推)

    ACM思维题训练集合 You've got an array a, consisting of n integers. The array elements are indexed from 1 to ...

随机推荐

  1. 微信小程序 使用include导入wxml文件注意的问题

    (1)使用inlucde的时,要注意将最后的终止符 / 添加上去,否则不能正常的导入界面内容 <include src="header.wxml"/> (2)引入文件注 ...

  2. "首字母变大写"组件:<capitalize> —— 快应用组件库H-UI

     <import name="capitalize" src="../Common/ui/h-ui/text/c_text_capitalize"> ...

  3. Python操作rabbitmq系列(三):多个接收端消费消息

    接着上一章.这一章,我们要将同一个消息发给多个客户端.这就是发布订阅模式.直接看代码: 发送端: import pikaimport sys connection = pika.BlockingCon ...

  4. spark本地开发环境搭建及打包配置

    在idea中新建工程 删除新项目的src,创建moudle 在父pom中添加spark和scala依赖,我们项目中用scala开发模型,建议scala,开发体验会更好(java.python也可以) ...

  5. Java 泛型、通配符? 解惑

    Java 泛型通配符?解惑 分类: JAVA 2014-05-05 15:53 2799人阅读 评论(4) 收藏 举报 泛型通配符上界下界无界 目录(?)[+] 转自:http://www.linux ...

  6. 安装JDK后,未设置Path,也能执行java.exe的原因

    安装JDK时,自动将java.exe复制到C:\Windows\System32下

  7. PDF阅读器

    1.SumatraPDF 非常小巧,打开很轻快 2.PDF Reader by Xodo window商店中可以找到,很好用 3. PDFXChange Editor这是我迄今为止见过的最好的PDF编 ...

  8. django 分页器 Paginator 基础操作

    基于下面这个分页器,说明常用的属性 from django.core.paginator import Paginator #导入Paginator类 from sign.models import ...

  9. testlink数据表分析

    创建一个项目test node_types: 节点类型,这是一个固定的表 rights: 权限表,用户的操作权限 role_rights: 角色和权限关系表 roles: 角色表 testprojec ...

  10. 使用redis-dump与redis-load方式迁移redis数据库

    实际生产场景中,有可能会因为迁移机房或者更换物理机等原因需要在生产环境迁移redis数据.本文就来为大家介绍一下迁移redis数据的方法. 迁移redis数据一般有如下3种方式: 1.第三方工具red ...