Codeforces 489A SwapSort
这题第一次看的时候以为是区间替换,后来发现看错了,只是单纯的元素替换。
解题思路:
先对输入的序列加个数组排个序
遍历下来,如果和排序后的结果当前元素不同,设当前位置为 i, 则往下面找,设查找位置为j
使得满足 a[j] == b[i] && a[j] != b[j]
一次遍历即可。易得证
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define ll long long
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
using namespace std; const int INF = 0x3f3f3f3f;
int a[], b[], n, counti;
int ans[][]; int main(){
int i, j, t, m;
while(EOF != scanf("%d",&n)){
counti = ;
for(i = ; i <= n; ++i){
scanf("%d",&a[i]);
b[i] = a[i];
}
sort(b + , b + + n);
for(i = ; i <= n; ++i){
if(a[i] != b[i]){
for(j = i + ; j <= n; ++j){
if(a[j] == b[i] && a[j] != b[j]){
swap(a[j], a[i]);
++counti;
ans[counti][] = j;
ans[counti][] = i;
break;
}
}
}
} printf("%d\n",counti);
for(i = ; i <= counti; ++i){
printf("%d %d\n",ans[i][] - , ans[i][] - );
}
} return ;
}
Codeforces 489A SwapSort的更多相关文章
- codeforces 489A.SwapSort 解题报告
题目链接:http://codeforces.com/problemset/problem/489/A 题目意思:给出一个 n 个无序的序列,问能通过两两交换,需要多少次使得整个序列最终呈现非递减形式 ...
- CodeForces 489A SwapSort (选择排序法)
SwapSort 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/A Description In this problem yo ...
- Codeforces 489A SwapSort (水题)
A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- 【CodeForces 489A】SwapSort
题 Description In this problem your goal is to sort an array consisting of n integers in at most n sw ...
- CodeForces 489A (瞎搞) SwapSort
题意: 给n个整数(可能有重复),输出一个不超过n次交换的方案,使得经过这n次交换后,整个序列正好是非递减的. 分析: 首先说题解给的算法. 从左到右扫一遍,交换第i个数和它后面最小的那个数. 代码看 ...
- Codeforces Round #277.5 (Div. 2)-A. SwapSort
http://codeforces.com/problemset/problem/489/A A. SwapSort time limit per test 1 second memory limit ...
- Codeforces Round #277.5 (Div. 2)A——SwapSort
A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- Codeforces Global Round 9 E. Inversion SwapSort
题目链接:https://codeforces.com/contest/1375/problem/E 题意 给出一个大小为 $n$ 的数组 $a$,对数组中的所有逆序对进行排序,要求按照排序后的顺序交 ...
- Codeforces Round #277.5 (Div. 2) ABCDF
http://codeforces.com/contest/489 Problems # Name A SwapSort standard input/output 1 s, 256 ...
随机推荐
- Java使用freemarker导出word和excel
www.linxiaosheng.com/post/2013-12-05/40060346181 https://github.com/upyun/java-sdk
- protel99se中做拼板图解
很多时候我们要在protel99se中做拼板, 但是通常在复制进行拼版的时候会出现如下的效果,元件被重新命名了. 而无法达到我们需要的像下图的效果 那我们怎么办,才能达到上图的效果呢?其实操作很简单. ...
- 完成端口(Completion Port)详解(超级长,超级清楚)
http://www.cnblogs.com/lancidie/archive/2011/12/19/2293773.html
- jvm学习小结
1. JDK.JRE.JVM之间的关系.JDK包含JRE和其它开发工具库如编译器.调试期,jConsele性能检测工具等2. JVM的构成:类装载器子系统.执行引擎.运行时数据区,如下图: 3. JV ...
- C++模板:读入优化
int scan(int &x){ while(c=getchar(),c<'0'||c>'9');x=c-'0'; while(c=getchar(),c>='0'& ...
- Linux 下的多线程编程
随着你对编程的深入,多线程是一个免不了的话题,在这里就对多线程做一个比较详细的总结. 首先摆在我们面前的就是什么是线程,以及为么会有这个东西.记得之前学习的时候自己会画一张很大的图,在图中可以详细的写 ...
- Qt5官方demo分析集11——Qt Quick Particles Examples - Affectors
在这个系列中的所有文章都可以在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 接上文Qt5官方demo解析集10--Qt ...
- CSS 初始化 代码
腾讯QQ官网 样式初始化 ;} body{font:12px"宋体","Arial Narrow",HELVETICA;background:#fff;-web ...
- [LeetCode]题解(python):009-Palindrome Number
题目来源: https://leetcode.com/problems/palindrome-number/ 题意分析: 这题是要判断一个int是否一个回文数,要求不能申请额外的空间. 题目思路: 这 ...
- [LeetCode]题解(python):022-Generate Parentheses
题目来源: https://leetcode.com/problems/generate-parentheses/ 题意分析: 题目输入一个整型n,输出n对小括号配对的所有可能性.比如说,如果输入3, ...