C. Little Elephant and Problem
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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".

题意:给出一个长为n的序列,问是否能够交换至多一次,使序列非降

#include<cstdio>
#include<algorithm>
using namespace std;
int n;
int a[],b[];
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b+n+);
int f=;
for(int i=;i<=n;i++)
if(a[i]!=b[i])
{
if(f<) f++;
else { puts("NO"); return ; }
}
puts("YES");
}

Codeforces 221 C. Little Elephant and Problem的更多相关文章

  1. Codeforces 221 B. Little Elephant and Numbers

    B. Little Elephant and Numbers time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  2. Codeforces 221 E. Little Elephant and Shifts

    E. Little Elephant and Shifts time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  3. Codeforces 221 D. Little Elephant and Array

    D. Little Elephant and Array time limit per test 4 seconds memory limit per test 256 megabytes input ...

  4. Codeforces 221 A. Little Elephant and Function

    A. Little Elephant and Function time limit per test 2 seconds memory limit per test 256 megabytes in ...

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

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

  6. Codeforces 221d D. Little Elephant and Array

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

  7. CF--思维练习--CodeForces - 221C-H - Little Elephant and Problem (思维)

    ACM思维题训练集合 The Little Elephant has got a problem - somebody has been touching his sorted by non-decr ...

  8. AC日记——Little Elephant and Problem codeforces 221c

    221C 思路: 水题: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...

  9. Codeforces Beta Round #17 A - Noldbach problem 暴力

    A - Noldbach problem 题面链接 http://codeforces.com/contest/17/problem/A 题面 Nick is interested in prime ...

随机推荐

  1. Android开发设计 实验报告

    20162315 Android开发设计 实验报告 实验内容 1.安装 Android Stuidio,完成Hello World, 要求修改res目录中的内容,Hello World后要显示自己的学 ...

  2. Java中的静态变量static

    package com.wangcf; public class Test { String name="你好"; static String sex="男"; ...

  3. 软件工程android项目简介

    我们的程序名字叫做“有爱”APP,英文名“you i”.意味着you and i,是一款旨在两人聊天,生活日记,记账工具,和对方通知的小软件. 1.首先我们的创意解决了用户什么需求? 答:在当今信息爆 ...

  4. 【转】node.js框架比较

    我偶然间看到这篇文章,转这个文章并没有什么含义,仅仅是感觉总结的不错,对于新学node的友友们来说希望这篇文章为大家对 Node.js 后端框架选型带来一些帮助,学习不再迷茫,也是让我有个保存,以后参 ...

  5. 配置java环境 启动服务

    1:查看当前的Java JDK版本,是否符合要求,下载的为2.4.4版本,因此满足条件 [root@7 ~]# java -version openjdk version "1.8.0_65 ...

  6. bash循环语句

    1  )单分支if语句 if 测试条件 :then 如果满足条件就执行这里的代码 f 2)双分支的if语句 if  测试条件:then 如果满足条件就执行这里的代码 else 如果不满足条件就执行这里 ...

  7. 小工具xml生成记录

    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent ...

  8. 分享:50行代码监听watch小程序的globalData

    监听方法: // 在任何组件.页面,例如页面 const app = getApp( ); Page({ onLoad: function( ) { app.watch$('role', ( val, ...

  9. SpringMVC Ajax两种传参方式

    1.采用@RequestParam或Request对象获取参数的方法 注:contentType必须指定为:application/x-www-form-urlencoded @ResponseBod ...

  10. 第119天:移动端:CSS像素、屏幕像素和视口的关系

    移动前端中常说的 viewport (视口)就是浏览器显示页面内容的屏幕区域.其中涉及几个重要概念是 dip ( device-independent pixel 设备逻辑像素 )和 CSS 像素之间 ...