当前位置:网站首页>1098 insertion or heap sort (25 points)
1098 insertion or heap sort (25 points)
2022-06-29 10:13:00 【Momo 623】
Answer key
Now the brain is slow , After reading Liu Shen's explanation , Her explanation is really beautiful
work through Don't feel superfluous code And it's very readable
Blog here , For personal notes only
And I feel that blogging helps clear your mind
Answer key
Two situations are required :
- Insertion sort : The first few elements are sequential , The last few elements are in the same order as the initial sequence
- Heap sort : The last few elements are sequential , The first few elements are a big top heap
solve :
- Insertion sort : Find two parts ( Sequence and are not sequence ) The dividing point of For the number of the first few elements +1 The number of March on sort
- Heap sort : Find the dividing point , It's easy to see ,b[1] Is the maximum value of the large top stack , It must be the minimum value of the ordered elements
so , From the back to the front
Code
#include <iostream>
#include <algorithm>
using namespace std;
const int n = 110;
int a[n], b[n];
int N;
int get_index()
{
int i = 1;
while (i <= N && b[i - 1] <= b[i])
i++;
int index = i;
while (i <= N && a[i] == b[i])
i++;
return (i == N + 1 ? index : 0);
}
int main()
{
cin >> N;
for (int i = 1; i <= N; i++)
cin >> a[i];
for (int i = 1; i <= N; i++)
cin >> b[i];
int index = get_index();
if (index)
{
cout << "Insertion Sort\n";
sort(b + 1, b + index + 1);
}
else
{
cout << "Heap Sort\n";
index = N;
while (index >= 2 && b[index] >= b[1])
index--;
swap(b[index], b[1]);
// Build heap functions
// The first few Just make a heap
make_heap(b + 1, b + index);
}
for (int i = 1; i <= N; i++)
{
if (i != 1)
cout << " ";
cout << b[i];
}
return 0;
}
边栏推荐
- Recyclerview refreshes blinks and crashes when deleting items
- 指针数组、数组指针和传参的相关问题
- Flutter 基础组件之 GridView
- 2019.10.27训练总结
- Ural1517 freedom of choice [suffix array: longest common continuous substring]
- Reverse thinking - short story
- In XML layout, the button is always displayed on the top layer
- L2-026 小字辈 (25 分)
- Using rancher to build kubernetes cluster
- Power Strings【KMP循环节】
猜你喜欢

Flutter 基础组件之 GridView

单片机集成开发环境Keil5的使用

Codeforces Round #645 (Div. 2)

JVM之方法返回地址

JVM之虚拟机栈之动态链接

指针数组、数组指针和传参的相关问题

Constructing SQL statements by sprintf() function in C language

Alibaba cloud firewall configuration, multiple settings (iptables and firewall)

Nacos environmental isolation

Alternative implementation of Scrollview pull-down header amplification
随机推荐
逆向思维-小故事
2019.10.20训练总结
Image of the basic component of the shutter
2019.10.23训练总结
Codeforces Round #641 Div2
信号作品:时变和时不变
Flutter 基础组件之 ListView
【51nod 1215】数组的宽度
Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning
子串分值-超详细版——最后的编程挑战
Nacos environmental isolation
如果我在北京,到哪里开户比较好?另外想问,现在在线开户安全么?
Time varying and non time varying
2019icpc上海区域赛赛后总结
Ural1517 freedom of choice [suffix array: longest common continuous substring]
单片机集成开发环境Keil5的使用
A 3D Dual Path U-Net of Cancer Segmentation Based on MRI
EDA and VHDL question bank
JVM之方法的绑定机制
2019-11-10训练总结