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 ...
随机推荐
- 为sublime安装package control 解决乱码问题 Mac版
为sublime安装package control Mac版参考 https://sublime.wbond.net/installation 防止中文乱码其实只需要2个东东 一个GBK enc ...
- JQuery 限制文本框只能输入数字和小数点
$(function(){ /*JQuery 限制文本框只能输入数字*/ $(".NumText").keyup(function(){ $(this).val($(this).v ...
- 在TreeWidget中增加右键菜单功能 以及TreeWidget的基本用法
TreeWidget 与 TreeView 中实现右键菜单稍有不同, TreeView 中是靠信号与槽 connect(ui->treeView,SIGNAL(customContextMenu ...
- Nginx 的 Echo 模块 —— echo-nginx-module(转)
Nginx 有个 echo 模块可以用来输出一些简单的信息,例如: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 ...
- DCI架构
提出的文章:DCI架构:一个面向对象编程的新图景 http://wenku.baidu.com/view/a7b5e401de80d4d8d15a4fed.html http://www.360doc ...
- http://qt-project.org/wiki/Category:Developing_with_Qt::QtWebKit#ff7c0fcd6a31e735a61c001f75426961
404: Not Found | Qt Project QtWebKit documentation http://dwz.cn/hr2013
- c语言实现tree数据结构
该代码实现了tree的结构.依赖dyArray数据结构.有first一级文件夹.second二级文件夹. dyArray的c实现參考这里点击打开链接 hashTable的c实现參考这里点击打开链接 ...
- android 中文 api (71) —— BluetoothServerSocket[蓝牙]
前言 本章内容是 android.bluetooth.BluetoothServerSocket,为Android蓝牙部分的章节翻译.服务器通讯套接字,与TCP ServerSocket类似.版本为 ...
- google chrome字体最小12px的问题
解决Google浏览器不支持12px以下的字体大小的问题,有时设定了12PX,可在浏览器看时确不起作用 网络出现内核的浏览器有微软的Internet Explorer, Mozilla的Firefox ...
- ServiceBase 类
https://msdn.microsoft.com/zh-cn/library/System.ServiceProcess.ServiceBase%28v=vs.80%29.aspx 为将作为服务应 ...