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 ...
随机推荐
- ssma for oracle
SQL Server Migration Assistant (SSMA) for Oracle lets you quickly convert Oracle database schemas to ...
- BeanUtils复制属性
package xiao; public class User2 { private String name; private String password; public String getNa ...
- mybatis foreach where test用法
<select id="selectAny" resultType="user" parameterType="user"> s ...
- inlay检验标准
Inlay 检验标准 检验条件及要求 正常的 40W 日光灯下距离被检物 50cm,眼睛距离被检物 30cm,与被检物呈 45 度角,目视检 使用强光灯箱透视其内部结构 适用范围 Inlay 中料 检 ...
- 在js中获取query string 以及重写URL的函数
函数用途:如标题.1. 从URL中解析出參数,2.重写URL中的參数值 例如以下代码所看到的.包括了測试.能够直接copy到浏览器中,输入測试地址:localhost:xxx?a=1&b=2& ...
- srm 534
250 Description 给你一个1*n的棋盘.两人轮流行动,每一个人能够把"o"向右移动到空格子.或者跨越连续两个"o"到空格子. 一个"o& ...
- 七、cocos2dx之粒子系统
本文由qinning199原创,转载请注明:http://www.cocos2dx.net/?p=66 一.介绍 粒子系统指的是一个使用大量很小的精灵或者其他的一些绘制对象模拟一些模糊效果.使用传统的 ...
- Aspx 页面生命周期
ASP.NET 页运行时,此页将经历一个生命周期,在生命周期中将执行一系列处理步骤.这些步骤包括初始化.实例化控件.还原和维护状态.运行事件处理程序代码以及进行 呈现.了解页的生命周期非常重要,这样就 ...
- 工具篇-TraceView
--- layout: post title: 工具篇-TraceView description: 让我们远离卡顿和黑屏 2015-10-09 category: blog --- ## 让我们远 ...
- JavaBean的一个小例子
一.创建一个javaBean类: UseBean package com.oncall24h.ruchi; import java.io.Serializable; public class UseB ...