码迷,mamicode.com
首页 > 编程语言 > 详细

CF489_A_插入排序

时间:2014-11-19 00:24:42      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

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

  AC代码:

bubuko.com,布布扣
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 const int maxn = 3000+10;
 9 int a[maxn], l[maxn], r[maxn];
10 int main() {
11     //freopen("test.txt", "r", stdin);
12     int n;
13     while(scanf("%d", &n)!=EOF) {
14         int ans = 0;
15         for(int i=0; i<n; i++)
16             scanf("%d", &a[i]);
17         for(int i=0; i<n; i++) {
18             int k = min_element(a+i, a+n)-a; // 找到[a+i, a+n]里面最小元素的下标
19             if(i!=k) {
20                 swap(a[i], a[k]);
21                 l[ans] = i; r[ans] = k; ans++;
22             }
23         }
24         printf("%d\n", ans);
25         for(int i=0; i<ans; i++) {
26             printf("%d %d\n", l[i], r[i]);
27         }
28     }
29     return 0;
30 }
View Code

看了别人写的,STL真是个好东西。。。

CF489_A_插入排序

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/sayeter/p/4106870.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!