首页 > 其他 > 详细

codeforces 221C Little Elephant and Problem

时间:2014-03-20 20:14:38      阅读:314      评论:0      收藏:0      [点我收藏+]

    题目的意思是说一个序列,如果能够通过一次交换任意两个元素,就能使得序列非递减,那么输出yes,否则输出no。使用a数组记录序列,然后复制到b数组,对b数组进行排序,然后比对两个数组,将不同元素的个数记下来,看是否超过两个就行了。

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
bool cmp(int a,int b)
{
    return a<b;
}

int main()
{
    int n,a[100010],b[100010];
    scanf("%d",&n);
    for(int i=0;i<n;i++)
       {
           scanf("%d",&a[i]);
           b[i]=a[i];
       }
    sort(a,a+n,cmp);
    int ans=0;
    for(int i=0;i<n&&ans<=2;i++)
        if(a[i]!=b[i]) ans++;
    if(ans<=2) printf("YES\n");
    else printf("NO\n");
    return 0;
}


 

codeforces 221C Little Elephant and Problem,布布扣,bubuko.com

codeforces 221C Little Elephant and Problem

原文:http://blog.csdn.net/knight_kaka/article/details/21643167

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!