当前位置:网站首页>3715. 最少交换次数
3715. 最少交换次数
2022-07-28 14:15: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 <bits/stdc++.h>
using namespace std;
const int N = 1010;
int w[N], ans[N], temp[N];
int n;
void merge_sort(int l, int r)
{
if (l >= r)
return;
int mid = l + r >> 1;
merge_sort(l, mid), merge_sort(mid + 1, r);
int i = l, j = mid + 1, k = 0;
while (i <= mid || j <= r)
{
if (j > r || i <= mid && w[i] <= w[j])
temp[k++] = w[i++];
else
{
ans[w[j]] += mid - i + 1;
temp[k++] = w[j++];
}
}
for (int i = l, j = 0; i <= r; i++, j++)
w[i] = temp[j];
}
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> w[i];
merge_sort(1, n);
int res = 0;
for (int i = 1; i <= n; i++)
{
cout << ans[i] << " ";
res += ans[i];
}
cout << endl
<< res << endl;
return 0;
}
边栏推荐
- Foundation of knowledge atlas (II) - knowledge expression system of knowledge atlas
- 云上安全主要面临的威胁有哪些
- CONDA create, CONDA install, CONDA update error conda.core.subdir_ data.Response304ContentUnchanged
- Instructions for common symbols in kotlin
- Compose learning notes 1-compose, state, flow, remember
- SystemVerilog
- Untitled may
- Image steganography method
- Robot mathematics foundation 3D space position representation space position
- C language exercises
猜你喜欢

C language related programming exercises

JS -- realize the rotation chart (complete function)

Qt development tips

The automatic prompt of vs code code is missing - a tick to solve it

Xiaobai can understand the 35 necessary questions in MySQL interview

Examples of Pareto optimality and Nash equilibrium

使用cpolar发布树莓派网页(apache2的安装测试)

Compose learning notes 1-compose, state, flow, remember

VTK notes - picker picker summary

Chapter II linear table
随机推荐
5、 C language judgment statement
C callback function, interface function pointer as function parameter, function pointer as structure member
使用cpolar发布树莓派网页(apache2网页的发布)
First class exercise
Tensorflow GPU installation process record
PS how to crop photos
Hard disk partition method
2、 Declaration and definition of variables and constants
Creating, deleting and viewing Anaconda virtual environment
Bcompare key expired or bcompare license key revoked
Process finished with exit code-1073740791(0xC0000409)
Shell command
The modified network card name of rocky foundation is eth0
7、 Detailed explanation of C language function definition
Keras reported an error using tensorboard: cannot stop profiling
linear transformation
Machine learning related concepts
21、 TF coordinate transformation (I): coordinate MSG message
Application of edge technology and applet container in smart home
How to conduct risk assessment related to intellectual property rights