这题第一次看的时候以为是区间替换,后来发现看错了,只是单纯的元素替换。

解题思路:

  先对输入的序列加个数组排个序

  遍历下来,如果和排序后的结果当前元素不同,设当前位置为 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的更多相关文章

  1. codeforces 489A.SwapSort 解题报告

    题目链接:http://codeforces.com/problemset/problem/489/A 题目意思:给出一个 n 个无序的序列,问能通过两两交换,需要多少次使得整个序列最终呈现非递减形式 ...

  2. CodeForces 489A SwapSort (选择排序法)

    SwapSort 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/A Description In this problem yo ...

  3. Codeforces 489A SwapSort (水题)

    A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  4. 【CodeForces 489A】SwapSort

    题 Description In this problem your goal is to sort an array consisting of n integers in at most n sw ...

  5. CodeForces 489A (瞎搞) SwapSort

    题意: 给n个整数(可能有重复),输出一个不超过n次交换的方案,使得经过这n次交换后,整个序列正好是非递减的. 分析: 首先说题解给的算法. 从左到右扫一遍,交换第i个数和它后面最小的那个数. 代码看 ...

  6. 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 ...

  7. 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 ...

  8. Codeforces Global Round 9 E. Inversion SwapSort

    题目链接:https://codeforces.com/contest/1375/problem/E 题意 给出一个大小为 $n$ 的数组 $a$,对数组中的所有逆序对进行排序,要求按照排序后的顺序交 ...

  9. Codeforces Round #277.5 (Div. 2) ABCDF

    http://codeforces.com/contest/489 Problems     # Name     A SwapSort standard input/output 1 s, 256 ...

随机推荐

  1. [LeetCode]题解(python):099-Recover Binary Search Tree

    题目来源: https://leetcode.com/problems/recover-binary-search-tree/ 题意分析: 二叉搜索树中有两个点错了位置,恢复这棵树. 题目思路: 如果 ...

  2. Array.Add () and += in PowerShell

    $newArray = @() $newArray.Add("Hello") If I create a new array, and using the method Add() ...

  3. ubuntu NFS

    NFS(Network File System, 网络文件系统)可以通过网络将分享不同主机(不同的OS)的目录——可以通过NFS挂载远程主机的目录, 访问该目录就像访问本地目录一样! 一般而言, 使用 ...

  4. C winpcap 网络抓包 并获取IP TCP 协议的相关信息

    以太网协议分析函数: void ethernet_protocol_packet_handle (u_char *argument, const struct pcap_pkthdr *packet_ ...

  5. dump文件定位程序崩溃代码行

    1.dump文件 2.程序对应的pdb 步骤一:安装windbg 步骤二:通过windbg打开crash dump文件 步骤三:设置pdb文件路径,即符号表路径 步骤四:运行命令!analyze -v ...

  6. 宣布在 Azure 镜像库中正式推出 Windows Server 2012 R2 并降低 Windows Azure 的实例定价

    我们今天将宣布两条消息,为使用基础结构服务的客户提供更多选择和成本节约:在镜像库中推出 Windows Server 2012 R2 以及降低 Memory Intensive 计算实例定价. 虚拟机 ...

  7. mac 下安装和卸载 mysql

    这里有一篇文章写得很详细: http://www.cnblogs.com/macro-cheng/archive/2011/10/25/mysql-001.html 关于卸载,我也百度了下.找了好几个 ...

  8. The Longest Straight(二分,离散化)

     Problem 2216 The Longest Straight Accept: 7    Submit: 14 Time Limit: 1000 mSec    Memory Limit : 3 ...

  9. struts2自己定义类型转换器

    1.1.  struts2自己定义类型转换器 1)        自定类型转换类,继承DefaultTypeConverter类 package com.morris.ticket.conversio ...

  10. #, about:blank,javascript:路径比较

    试了一下在<a>,<img>,<iframe>中用#,about:blank和javascript: 代码如下: <!Doctype html> < ...