题目链接: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. c++实现快排出现错误

    #include"header_file.h" using namespace std; void swap(int a,int b) { int t; t=a; a=b; b=t ...

  2. android选择时间攻略

    安卓开发过程中难免会碰到需要选择日期时间的情况,由于大部分android初级教程都没教怎么选择时间,初学者碰到这种难免会有些不知所措,难道要让用户自己输入日期时间?先不说用户体验不好,处理用户输入各式 ...

  3. 快速反射DataTable

    public class SetterWrapper<TTarget, TValue> { private Action<TTarget, TValue> _setter; p ...

  4. vs------安装window net.framework 出现严重错误解决方法

    打开system32和SysWOW64修改里面共六个msvcr100_clr0400.dll文件的名字除了带120的文件不用修改 需要修改管理员权限: 步骤:属性->安全->编辑-> ...

  5. 将Spark中CompactBuf转换为String

    val rdd = sc.textFile("hdfs://hbase11:9000/sparkTsData/ipsoftware/wincc").map{ line => ...

  6. php中单例模式的解析说明

    <?php //单例模式 class Dbconn{ private static $_instance=null; protected static $_counter=0; protecte ...

  7. Struts2标签简介

    Struts2标签简介 Struts2标签的优势 标签库简化了用户对标签的使用 结合OGNL使用,对于集合.对象的访问功能非常强大 提供可扩展的主题.模板支持,极大简化了视图页面的编写 不依赖任何表现 ...

  8. 我用的/etc/vimrc

    " 映射非数字/字母键, 如:ctrl,shift, alt, home,end,功能键F1~F12, 要把这些键用尖括号括起来!如: map <F3> :NERDTree< ...

  9. linux 中文件夹的文件按照时间倒序或者升序排列

    1,按照时间升序 命令:ls -lrt 详细解释: -l use a long listing format 以长列表方式显示(详细信息方式) -t sort by modification time ...

  10. Hadoop Streaming例子(python)

    以前总是用java写一些MapReduce程序现举一个例子使用Python通过Hadoop Streaming来实现Mapreduce. 任务描述: HDFS上有两个目录/a和/b,里面数据均有3列, ...