Codeforces 489A SwapSort (水题)
1 second
256 megabytes
standard input
standard output
In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another.
Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.
The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of array elements. The second line contains elements of array: a0, a1, ..., an - 1 ( - 109 ≤ ai ≤ 109), where ai is the i-th element of the array. The elements are numerated from 0 to n - 1 from left to right. Some integers may appear in the array more than once.
In the first line print k (0 ≤ k ≤ n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers i, j (0 ≤ i, j ≤ n - 1), representing the swap of elements ai and aj. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = jand swap the same pair of elements multiple times.
If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.
5
5 2 5 1 4
2
0 3
4 2
6
10 20 20 40 60 60
0
2
101 100
1
0 1 水题,排个序,然后逐个对比下,如果a[i]==b[i]的话,就不用管,如果a[i]!=b[i]的话,就找一下与a[i]数值相同的,换一下位置
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1e9;
const double eps = 1e-;
const int N = ;
int cas = ; int a[N],b[N],n;
vector<pii> ans; inline void sw(int &x,int &y)
{
int tmp = x;
x=y;
y=tmp;
} void run()
{
ans.clear();
for(int i=;i<n;i++) scanf("%d",a+i);
for(int i=;i<n;i++) b[i]=a[i];
sort(b,b+n);
int p;
for(int i=;i<n;i++)
{
if(a[i]==b[i]) continue;
for(p=i+;p<n;p++)
if(a[p]!=b[p] && a[p]==b[i]) break;
sw(a[i],a[p]);
ans.push_back(make_pair(i,p));
}
printf("%d\n",ans.size());
for(int i=;i<ans.size();i++)
printf("%d %d\n",ans[i].first,ans[i].second);
} int main()
{
#ifdef LOCAL
// freopen("case.txt","r",stdin);
#endif
while(scanf("%d",&n)!=EOF)
run();
return ;
}
Codeforces 489A SwapSort (水题)的更多相关文章
- Codeforces数据结构(水题)小结
最近在使用codeblock,所以就先刷一些水题上上手 使用codeblock遇到的问题 1.无法进行编译-------从setting中的编译器设置中配置编译器 2.建立cpp后无法调试------ ...
- CodeForces 705A Hulk (水题)
题意:输入一个 n,让你输出一行字符串. 析:很水题,只要判定奇偶性,输出就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,10240 ...
- CodeForces 705B (训练水题)
题目链接:http://codeforces.com/problemset/problem/705/B 题意略解: 两个人玩游戏,解数字,一个数字可以被分成两个不同或相同的数字 (3可以解成 1 2) ...
- codeforces 489A.SwapSort 解题报告
题目链接:http://codeforces.com/problemset/problem/489/A 题目意思:给出一个 n 个无序的序列,问能通过两两交换,需要多少次使得整个序列最终呈现非递减形式 ...
- codeforces hungry sequence 水题
题目链接:http://codeforces.com/problemset/problem/327/B 这道题目虽然超级简单,但是当初我还真的没有想出来做法,囧,看完别人的代码恍然大悟. #inclu ...
- codeforces 377A. Puzzles 水题
A. Puzzles Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/33 ...
- CodeForces 489A SwapSort (选择排序法)
SwapSort 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/A Description In this problem yo ...
- Codeforces 489A SwapSort
这题第一次看的时候以为是区间替换,后来发现看错了,只是单纯的元素替换. 解题思路: 先对输入的序列加个数组排个序 遍历下来,如果和排序后的结果当前元素不同,设当前位置为 i, 则往下面找,设查找位置为 ...
- CodeForces 474B Worms (水题,二分)
题意:给定 n 堆数,然后有 m 个话询问,问你在哪一堆里. 析:这个题是一个二分题,但是有一个函数,可以代替写二分,lower_bound. 代码如下: #include<bits/stdc+ ...
随机推荐
- EntityFramework走马观花之CRUD(中)
如果是独立的实体对象,在底层数据库中它对应一张独立的表,那么,对它进行新建.删除和修改没有任何难度,实在不值浪费笔墨在它上头. 在现实项目中,完全独立的对象少之又少,绝大多数情况都是对象之间有着紧密的 ...
- 再看GS接包过程
再看GS接包过程 bool GameServer::ProcessLoop(packet& rPkt) { if(false == m_spDataLayer->Recv(rPkt)) ...
- python书写日志的重要性?
转自:https://blog.csdn.net/weixin_43063753/article/details/82899395 程序为什么要写日志?#为了能够在程序在运行过程中记录错误,方便维护, ...
- Tomcat学习笔记【3】--- Tomcat目录结构
本文主要讲Tomcat包的目录结构. 1 bin目录 这个目录只要是存放了一些bat文件或者sh文件.比如说我们需要启动tomcat的bat文件就在这个目录下. 2 conf 这个目录中存放的都是一些 ...
- RDLC报表-分组序号
1.RowNumber("group_name")是按行每个分组重新1,2,3这样来显示的,如果需要按每个分组来显示1,并且递增,则需要通过自定义代码来控制,在报表-属性-代码里插 ...
- javascript 跨浏览器事件处理
<div id="myDiv" style="width:100px; height:100px; border:1px solid #f00;"> ...
- nginx高可用配置
可参考资料: http://www.cnblogs.com/holbrook/archive/2012/10/25/2738475.html http://blog.csdn.net/e4210834 ...
- tkinter之button
Button按钮,直接上代码: from tkinter import * def gs(): global read s=Label(read,text='昨夜西风凋敝树,堵上高楼,望尽天涯路!', ...
- Cocos2d-x中单例的使用
大家都知道一个程序中只有一个导演类,eg: CCDirector *pDirectory = CCDirector::sharedDirector();//初始化导演类. 可通过 pDirectory ...
- 淘宝双十一页面(Flexible)demo
下面的代码是看了大漠 使用Flexible实现手淘H5页面的终端适配 做的一个demo. <!DOCTYPE html> <html lang="en" ng-a ...