当前位置:网站首页>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 通用适配器封装

点在多边形内外的判断

JVM之虚拟机栈之动态链接

Automatic Multi-Organ SegmVentation on Abdominal CT With Dense V-Networks

Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning

Nacos环境隔离

两个栈的模拟题

Image of the basic component of the shutter

The Stones Game【取石子博弈 & 思维】

函数指针、函数指针数组、计算器+转移表等归纳总结
随机推荐
Alibaba cloud firewall configuration, multiple settings (iptables and firewall)
Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning
2019.10.6训练总结
RecyclerView刷新闪烁与删除Item时崩溃问题
The collapsing "2.3 * 10 = 22" produced by multiplying float and int
The stones game
2019.11.20训练总结
Codeforces Round #641 Div2
L2-031 深入虎穴 (25 分)
另类实现 ScrollView 下拉头部放大
弧形 View 和弧形 ViewPager
Shanke's C language 2018 exercise (Telecom)
Middle order traversal of Li Kou 94 binary tree
Judgment of points inside and outside polygon
Pointer functions and function pointers
2019.10.23训练总结
GSOAP example - calc
51nod1277 maximum value in string [KMP]
内网穿透工具frp使用入门
Related problems of pointer array, array pointer and parameter passing