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 ...
随机推荐
- hdu 4619 Warm up 2 二分图匹配
题目链接 给两种长方形, 水平的和垂直的, 大小都为1*2, n个水平的, m个垂直的, 给出它们的坐标. 水平的和垂直的可以相互覆盖, 但是同种类型的没有覆盖. 去掉一些长方形, 使得剩下的全部都没 ...
- mabatis mapper开发规范
定义一个mapper接口 package mapper; import pojo.User; public interface UserMapper { public User getUser(int ...
- wsdl透明解析
1.逐个分析wsdl文件中的元素: <types>:数据类型定义的容器,一般使用 xml schema类型系统. <message>:通信消息的数据结构的抽象化定义,使用< ...
- (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示
原文 (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示 (本系列文章由柠檬的(lc_mtt)原创,转载请注明出处,谢谢-) 接上一节:(C#)Windows She ...
- lua的前景??
除了专业的游戏公司,貌似很少人在用lua来做开发啊,国内的lua社区越来越不行了. lua还在不断的发展,但每次新版本c接口都改动很大,项目想要升级有点困难啊. lua还有前途吗?
- struts之拦截器
拦截器是为了让一些自己不希望发生的事情进行预防.以下我说一下struts自己定义拦截器. 以下我贴下struts.xml里的自定义的拦截器: <package name="my&quo ...
- [python网络编程]DNSserver
在上一篇中,使用scrapy改动源IP发送请求的最后我们提到因为hosts文件不支持正则,会导致我们的随机域名DNS查询失败. 使用DNS代理服务器能够解决问题, 以下是我用gevent写的小工具.非 ...
- junit学习笔记(二):hamcrest和TestSuit
1. hamcrest hamcrest可以有效增加junit的测试能力,用一些对通俗语言来进行测试. Hamcrest 是一个测试的框架,它提供了一套通用的匹配符 Matcher,灵活使用这些匹配符 ...
- python第三方模块
python相关:1.zeromq网络库:2.twisted框架:twisted:一个基于事件驱动,异步的python高性能网络开发框架:注:什么是基于事件驱动:当(鼠标点击事件)事件注册器注入事件, ...
- Ext JS学习第十天 Ext基础之 扩展原生的javascript对象(二)
此文来记录学习笔记: 今天继续说Ext.Array,Ext.Function,Ext.Date,Ext.Error ------------------------------------------ ...