当前位置:网站首页>Acwing3715. 最少交换次数(冒泡排序法的模拟思路)
Acwing3715. 最少交换次数(冒泡排序法的模拟思路)
2022-07-27 18:56:00 【理工大猪猪】
给定一个 1∼N 的随机排列,要求一次只能交换相邻两个数,那么最少需要交换多少次才可以使数列按照从小到大排列呢?
请你求出一个待排序序列的最少交换次数和对应的逆序数列。
逆序数列:给定 n 个数 1,2,…,n 的一个排列 a1a2…an,令 bi 是数 i 在此排列中的逆序数,换句话说,bi 等于该排列中先于 i 又大于 i 的那些数的个数。数列 b1b2…bn 称为排列 a1a2…an 的逆序数列(inversion sequence)。
输入格式
第一行一个整数 N。
第二行一个 1∼N 的排列。
输出格式
第一行输出逆序数列,数之间用空格隔开。
第二行输出最少交换次数。
数据范围
1≤N≤1000
输入样例:
8
4 8 2 7 5 6 1 3
输出样例:
6 2 5 0 2 2 1 0
18要让数列有序, 逆序对中的元素必然直接或间接变换相对位置, 而题目中只能相邻元素交换 ,因此每次交换最多只能消除一个逆序对 所以最小交换次数即逆序对的数量(逆序数列的和)
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e3 + 10;
int n, res;
int a[N], b[N];
int main()
{
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> a[i];
for (int i = 1; i <= n; ++i)
for (int j = 1; j < i; ++j)
if (a[j] > a[i])
b[a[i]]++;
for (int i = 1; i <= n; ++i)
printf("%d ", b[i]), res += b[i];
printf("\n%d\n", res);
return 0;
}
算法思路
对于第一个小问题,直接暴力模拟冒泡排序。
对于第二个小问题,现在数组中找到对应的数字,然后纯枚举即可。
时间复杂度
冒泡排序复杂度是O(n^2)的。找到数字和枚举都是线性的,加上序列枚举,在O(n^2)左右。综上,时间复杂度为O(n^2)。
边栏推荐
- Mysql 数据恢复流程 基于binlog redolog undolog
- Summary of common methods and attributes of arrays and strings in JS
- Worthington毒液中核酸外切酶的特征及相关文献
- Guava Cache 原理分析与最佳实践
- Win11系统更新KB5014668后点开始按钮没反应怎么办?
- Multi person collaborative development specification
- 大佬们,mysql版本低,不支持cdc,所以canal同步binlog至kafka,数据同步至cli
- How to speed up the memory database through special data type index
- 激光雷达中国前装大幕开启,数百万颗产能待消化
- Paper appreciation [emnlp18] dynamic oracles for top-down and middle order move in reduction component parsing
猜你喜欢

新来个技术总监要我做一个 IP 属地功能~

Multi person collaborative development specification

基于DSP 回传音通话降噪链路设计

电脑微软账户登录一直转圈怎么解决问题

Can China make a breakthrough in the future development of the meta universe and occupy the highland?

Leetcode daily practice - 21. Merge two ordered linked lists

ECCV 2022 | China University of science and Technology & jd.com proposed: data efficient transformer target detector

mysql 最大建议行数2000w,靠谱吗?

A review of component parsing (Second Edition)

飞桨框架体验评测交流会,产品的使用体验由你来决定!
随机推荐
A new technical director asked me to do an IP territorial function~
API gateway introduction
Five celebrities' worries about AI
PHP code audit 5 - XSS vulnerability
Worthington血浆胺氧化酶 (PAO) 说明书
Leetcode daily practice 206. Reverse the linked list
ADB shell LS /system/bin (index table)
CBAM学习笔记
The new CTO strongly prohibits the use of calendar?
Set up discuz forum and break the stolen database
Chinese and English instructions - human alpha fetoprotein (AFP) ELISA quantitative Kit
Leetcode daily practice - the penultimate node in the linked list
Simple use of express web server
R language uses power.for 2p function performs utility analysis (efficiency analysis, power analysis), calculates the utility value given the proportions of two samples and the sample size
Multi person collaborative development specification
Smart Internet ran out of China's "acceleration", and the market reshuffle behind the 26.15% carrying rate
Typoa spelling check: missing dictionary file for Chinese
App test positioning method
[what subjects does Huawei hcie security test? What knowledge points does Huawei hcie security test have?]
The solution that the laptop can connect to WiFi but the browser cannot open the web page