当前位置:网站首页>3715. Minimum number of exchanges
3715. Minimum number of exchanges
2022-07-28 15:13:00 【Pursue people far away】
Given a 1∼N The random arrangement of , It is required that only two adjacent numbers can be exchanged at a time , So how many times do you need to exchange at least to make the sequence order from small to large ?
Please find out the minimum exchange times of a sequence to be sorted and the corresponding reverse sequence .
Reverse sequence : Given n Number 1,2,…,n An arrangement of a1a2…an, Make bi Yes number i The number in reverse order in this arrangement , let me put it another way ,bi Equal to before i More than i The number of those numbers . The sequence b1b2…bn It's called permutation a1a2…an The reverse sequence of (inversion sequence).
Input format
The first line is an integer N.
One in the second line 1∼N Permutation .
Output format
The first row outputs the reverse sequence , The numbers are separated by spaces .
The second line outputs the minimum number of exchanges .
Data range
1≤N≤1000
sample input :
8
4 8 2 7 5 6 1 3
sample output :
6 2 5 0 2 2 1 0
18
Code :
// The number of bubble sort exchanges is equal to the number of reverse pairs
/* To keep the sequence in order , The elements in the reverse order pair must directly or indirectly change their relative positions , In the title, only adjacent elements can be exchanged , Therefore, each exchange can only eliminate one reverse pair at most So the minimum number of exchanges is the number of pairs in reverse order ( Sum of reverse sequence ) */
#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;
}
边栏推荐
- Process finished with exit code-1073740791(0xC0000409)
- 20、 ROS distributed communication
- PS how to crop photos
- [complete installation package & tutorial] sqlserver basic installation_ Sqlserver completely uninstalled_ Sqlserver custom installation_ Getting started with sqlserver_ SQLSERVER database
- Touch hands to realize canal how to access Mysql to realize data write operation monitoring
- CONDA create, CONDA install, CONDA update error conda.core.subdir_ data.Response304ContentUnchanged
- DataTables warning: table id=campaignTable - Cannot reinitialise DataTable.解决
- Why do enterprises need user autonomous digital identity
- 3477. Simple sorting
- 23、 TF coordinate transformation (III): dynamic coordinate transformation
猜你喜欢

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

PHP memory horse

MLX90640 红外热成像仪传感器模块开发笔记(八)

PS modify the length and width pixels and file size of photos

Xiaobai can understand the 35 necessary questions in MySQL interview

滑块还原和验证(法律数据库)

Examples of Pareto optimality and Nash equilibrium

Repvgg paper explanation and model reproduction using pytoch

Chapter I Introduction

Machine learning related concepts
随机推荐
15、 Launch file label of ROS (I)
4519. 正方形数组的数目
Some considerations for installing Oracle11g
Shader顶点着色器修改顶点高度的一个思路
Solve blast database error: error pre fetching sequence data
Why do enterprises need user autonomous digital identity
CCSP international registered cloud security experts set up examination rooms in China
PHP memory horse
16、 Launch file label of ROS (II)
Feeling about software development work in the second anniversary
Find papers and their open source code
安装mosek,license安装位置查找
QT hex, decimal, qbytearray, qstring data conversion
Idea2020.1.4 packages package collapse
When MySQL uses left join to query tables, the query is slow because the connection conditions are not properly guided
MLX90640 红外热成像仪传感器模块开发笔记(八)
Search Pfam with Hmmer
6、 C language circular statement
JS study notes 18-23
RepVGG论文详解以及使用Pytorch进行模型复现