题目链接:http://codeforces.com/problemset/problem/489/A

题目意思:给出一个 n 个无序的序列,问能通过两两交换,需要多少次使得整个序列最终呈现非递减形式。这个交换次数最多为 n 次。如果原来已经非递减排列好,输出次数为0;否则不但要输出次数,还需要输出每次交换的数组下标。

  比赛的时候想复杂了,用了pair来记录值和坐标,还要开多一个pair类型的 b 数组来保存已排好序的序列,然后跟原序列比较......哪个复杂啊~~~然后校园网12:00 断网了,比原来提早半小时 = =,不过都好,原来的做法是错的。

其实之所以没有向简单的方法想,是因为被最多 n 次这个条件吓到了,没有仔细想清楚,以为两两比较最终会超 n 次。其实是不会的。

  遍历 0 ~ n-1 的位置,选出最小,然后次小,第三小,.....,直到最大。如果发现当前位置放置的数是正确的,就continue,最多最多交换次数是 O(n) !

15ms 版本

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
int a[maxn];
int res[maxn][]; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int n; while (scanf("%d", &n) != EOF)
{
for (int i = ; i < n; i++)
scanf("%d", &a[i]);
int cnt = ;
for (int i = ; i < n; i++)
{
int minn = a[i];
int choice = i;
for (int j = i+; j < n; j++)
{
if (a[j] < minn)
{
minn = a[j];
choice = j;
}
}
if (choice == i)
continue;
res[cnt][] = i;
res[cnt++][] = choice;
swap(a[i], a[choice]);
}
printf("%d\n", cnt);
for (int i = ; i < cnt; i++)
printf("%d %d\n", res[i][], res[i][]);
}
return ;
}

至于下面这个呢,是参考乌冬子写的,用到 STL 中的 min_element(first, end, cmp),其中cmp为可选择参数!

表示在[first, end) (看清楚,这个是左闭右开区间)中查找最小的元素。max_element则是查找最大元素。

  这个函数我也是第一次见,*min_element(first, end, cmp)代表这个最小的元素的值。

假设数组用a[]来表示。min_element(a[i], a[i]+n) - a  才返回最小元素的值的下标!!!

  乌冬子的做法还有一个巧妙的地方,就是即使元素放置位置正确,也原地输出交换下标,即类似输出 i i 这种。而且还有一点就是不需要开多一个数组来存储答案。真应该给个大大的赞他呀~~~~^_^,有一个稍稍瑕疵就是时间用多了,需要 31ms !STL 普遍都是这样的。

  31ms 偷师版

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
int a[maxn]; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int n; while (scanf("%d", &n) != EOF)
{
for (int i = ; i < n; i++)
scanf("%d", &a[i]);
printf("%d\n", n);
for (int i = ; i < n; i++)
{
int j = min_element(a+i, a+n) - a;
printf("%d %d\n", i, j);
swap(a[i], a[j]);
}
}
return ;
}

codeforces 489A.SwapSort 解题报告的更多相关文章

  1. codeforces 31C Schedule 解题报告

    题目链接:http://codeforces.com/problemset/problem/31/C 题目意思:给出 n 个 lessons 你,每个lesson 有对应的 起始和结束时间.问通过删除 ...

  2. codeforces 499B.Lecture 解题报告

    题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 profes ...

  3. codeforces 495C. Treasure 解题报告

    题目链接:http://codeforces.com/problemset/problem/495/C 题目意思:给出一串只有三种字符( ')','(' 和 '#')组成的字符串,每个位置的这个字符 ...

  4. codeforces 490B.Queue 解题报告

    题目链接:http://codeforces.com/problemset/problem/490/B 题目意思:给出每个人 i 站在他前面的人的编号 ai 和后面的人的编号 bi.注意,排在第一个位 ...

  5. CodeForces 166E -Tetrahedron解题报告

    这是本人写的第一次博客,学了半年的基础C语言,初学算法,若有错误还请指正. 题目链接:http://codeforces.com/contest/166/problem/E E. Tetrahedro ...

  6. codeforces 485A.Factory 解题报告

    题目链接:http://codeforces.com/problemset/problem/485/A 题目意思:给出 a 和 m,a 表示第一日的details,要求该日结束时要多生产 a mod ...

  7. codeforces 483A. Counterexample 解题报告

    题目链接:http://codeforces.com/problemset/problem/483/A 题目意思:给出一个区间 [l, r],要从中找出a, b, c,需要满足 a, b 互质,b, ...

  8. codeforces 479C Exams 解题报告

    题目链接:http://codeforces.com/problemset/problem/479/C 题目意思:简单来说,就是有个人需要通过 n 门考试,每场考试他可以选择ai, bi 这其中一个时 ...

  9. codeforces 479B Towers 解题报告

    题目链接:http://codeforces.com/problemset/problem/479/B 题目意思:有 n 座塔,第 i 座塔有 ai 个cubes在上面.规定每一次操作是从最多 cub ...

随机推荐

  1. Ubuntu安装VMware Tools的方法

    最后我将提供一个12版本的VMware Tools集合,包括了linux.iso. 背景: VMware Tools是VMware虚拟机中自带的一种增强工具,相当于VirtualBox中的增强功能(S ...

  2. Spring AOP中定义切点(PointCut)和通知(Advice)

    如果你还不熟悉AOP,请先看AOP基本原理,本文的例子也沿用了AOP基本原理中的例子.切点表达式 切点的功能是指出切面的通知应该从哪里织入应用的执行流.切面只能织入公共方法.在Spring AOP中, ...

  3. Message

    * Defines a message containing a description and arbitrary data object that can be* sent to a {@link ...

  4. pthread_create线程创建的过程剖析

    http://blog.csdn.net/wangyin159/article/details/47082125 在Linux环境下,pthread库提供的pthread_create()API函数, ...

  5. 嵌入式实时操作系统μCOS原理与实践+事件部分代码

    //事件等待表的初始化函数:pevent表示事件控制块的指针#if (OS_EVENT_EN)void  OS_EventWaitListInit (OS_EVENT *pevent){    INT ...

  6. 2层Xml读取类

    配置文件 <?xml> <root> <parent name="C"> <child name="C1">Sp ...

  7. RecyclerView的使用方法

    1.添加.jar包 File -> Project-Structure -> 左下角 “app” -> Dependencies -> 右上角“+” -> File de ...

  8. ubuntu下安装 openssl 开发库

    ubuntu下安装 openssl 开发库 检查是否已安装openssl: sudo apt-get install openssl 如果已安装执行以下操作:sudo apt-get install ...

  9. 用css3制作旋转加载动画的几种方法

    以WebKit为核心的浏览器,例如Safari和Chrome,对html5有着很好的支持,在移动平台中这两个浏览器对应的就是IOS和Android.最近在开发一个移动平台的web app,那么就有机会 ...

  10. afasf

    http://www.cnblogs.com/ttzhang/archive/2008/11/02/1324601.html project server 2007 sn :W2JJW-4KYDP-2 ...