当前位置:网站首页>Acwing3715. Minimum exchange times (simulation idea of bubble sorting method)
Acwing3715. Minimum exchange times (simulation idea of bubble sorting method)
2022-07-27 21:26:00 【Science and technology big pig】
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
18To keep the sequence in order , The elements in the reverse order pair must directly or indirectly change their relative positions , And 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 <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;
}
Algorithm ideas
For the first small problem , direct Violent simulation bubble sort .
For the second small problem , Now find the corresponding number in the array , Then just enumerate .
Time complexity
Bubble sort complexity is O(n^2) Of . Find that both numbers and enumerations are linear , Plus sequence enumeration , stay O(n^2) about . Sum up , The time complexity is O(n^2).
边栏推荐
- [anti shake and throttling]
- 新来个技术总监要我做一个 IP 属地功能~
- Postgresql源码(65)新快照体系Globalvis工作原理分析
- Sre: Google operation and maintenance decryption
- ECCV 2022 | China University of science and Technology & jd.com proposed: data efficient transformer target detector
- Understand the communication mode of transmission media
- The use experience of the product is up to you in the evaluation and exchange meeting of the flying oar frame experience!
- ORA-27300,ORA-27301,ORA-27302,ORA-27303,TNS-2518,TNS-12549,TNS-12560,TNS-00519等告警处理
- R language uses LM function to build multiple linear regression model, writes regression equation according to model coefficient, and uses deviance function to calculate the sum of squares of residual
- R language uses dplyr package to perform data aggregation statistics, calculate sliding window statistics, calculate sliding group mean, and merge the generated statistical data into the original data
猜你喜欢

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

使用百度飞桨EasyDL实现电商UGC图片自动分类

PostgreSQL source code (65) analysis of the working principle of globalvis, a new snapshot system

The new CTO strongly prohibits the use of calendar?

成分句法分析综述(第二版)

Digital leading planning first, focusing on the construction of intelligent planning information platform and the exploration and practice of application projects

Dobot Magician 机器臂-简介

Automatic classification of e-commerce UGC pictures using Baidu PaddlePaddle easydl

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

ACM mm 2022 | Zhejiang University proposed: point cloud segmentation, active learning of new SOTA
随机推荐
Some operations about Anaconda (installing software and quickly opening)
Zhongdi Digital: integrating innovative domestic GIS to boost the construction of real 3D China
Paper appreciation [emnlp18] dynamic oracles for top-down and middle order move in reduction component parsing
Characteristics and determination scheme of Worthington mushroom polyphenol oxidase
Summary of common methods and attributes of arrays and strings in JS
ADB ~ 隐藏或禁用状态栏和虚拟按键
Leetcode daily practice - the penultimate node in the linked list
Behavior level description and RTL level description
中英文说明书丨 AbFluor 488 细胞凋亡检测试剂盒
Understanding of reg type variables in Verilog HDL
Force buckle 919. Complete binary tree inserter
The dplyr package of R language performs aggregation transformations of data packets and calculates the sum of packets of dataframe data
Thinking about SLA of cloud computing
Traps and limitations of Engineering Technology Development
报表设计丨如何让你的PowerBI看板出彩?
[R language] [1] beginners learn the grammar of R language and edit it with rstudio
Knife4j dynamically refreshes global parameters through JS
数字化工厂系统有什么现实优势
Chapter 7 Intermediate Shell Tool I
Worthington蘑菇多酚氧化酶的特性及测定方案