1098. Insertion or Heap Sort (25)

时间限制


100 ms


内存限制


65536 kB


代码长度限制


16000 B


判题程序


Standard


作者


CHEN, Yue


According to Wikipedia:

Insertion sort

Heap sort

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorting method, can you tell which sorting method we are using?

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=100). Then in the next line, N integers are given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed that the target sequence is always ascending. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in the first line either "Insertion Sort" or "Heap Sort" to indicate the method used to obtain the partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed that the answer is unique for each test case. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:

10 3 1 2 8 7 5 9 4 6 0 1 2 3 7 8 5 9 4 6 0

Sample Output 1:

Insertion Sort 1 2 3 5 7 8 9 4 6 0

Sample Input 2:

10 3 1 2 8 7 5 9 4 6 0 6 4 5 1 0 3 2 7 8 9

Sample Output 2:

Heap Sort 5 4 3 1 0 2 6 7 8 9


输入N接下来有两行
一行原始无序的
第二行在两种排序过程的某一步结果;
求再做一步的结果是甚么;
Insert sort 直接插入排序,当N>1时;进行N-1循环,每次的主角为2~N;
每次把主角和与他前一个位置的配角比较,如果主角较小,把主角当前位置给与他比较的配角,并占据配角的位置,继续直到前面没有配角;
否则这一次结束;
Heap Sort 堆排序(PS:我代码中有建立初始堆,但是测试没有建立初始堆的那一步也是可以AC的2015-8-3)
每次:
假如堆已经建好,第一个永远是最大,把它和最后一个交换,当前队伍减少一个;
主角为第一个,他的一个或两个配角,取配角中比较大的,然后和主角比较,如果比主角大,占据主角的位置,主角占据配角的位置,继续直到下面没有配角
否则停止;


评测结果

时间

结果

得分

题目

语言

用时(ms)

内存(kB)

用户

8月03日 12:07

答案正确

​25​

​1098​

​C++ (g++ 4.7.2)​

1

384

​datrilla​

测试点

测试点

结果

用时(ms)

内存(kB)

得分/满分

0

答案正确

1

252

7/7

1

答案正确

1

384

6/6

2

答案正确

1

384

2/2

3

答案正确

1

384

2/2

4

答案正确

1

252

4/4

5

答案正确

1

300

4/4

#include<iostream>
#include<algorithm>
using namespace std;
void readlninitial(int *initial,int N)
{
for (int index = 0; index < N;)
{
index++;
cin >> initial[index];
}
}
bool readln_IsInsert(int *initial, int N, int*partial, int*squenseIncIndex)
{
bool mark = true;
int index;
partial[0]= initial[0] = 0;
(*squenseIncIndex) = 0;
for (index = 0; index < N;)
{
index++;
cin >> partial[index];
if (mark && (index >1&&partial[index] >= partial[index - 1]||index==1))
(*squenseIncIndex) = index;
else
mark = false;
}
while (index>(*squenseIncIndex))
{
if (partial[index] != initial[index])return false;
index--;
}
return true;
}
void InsertSort(int*partial, int squenseIncIndex)
{
int IsIndex = squenseIncIndex + 1;
int IsTurn = partial[IsIndex];
bool NOfindPlace=true;
for (; squenseIncIndex >= 0&&NOfindPlace; squenseIncIndex--)
{
if (partial[squenseIncIndex] <= IsTurn)
{
partial[IsIndex] = IsTurn;
NOfindPlace = false;
}
else
{
partial[IsIndex] = partial[squenseIncIndex];
IsIndex = squenseIncIndex;
}
}
}
bool RBCMP(const int&A, const int&B)
{
return A < B;
}
void rebuild_heap(int*partial, int N, int Root)
{
int leaves_1;
bool NO_Ok = true;
partial[0] = partial[Root];
leaves_1 = Root*2;
while (leaves_1 <=N&&NO_Ok)
{
if (leaves_1 < N && partial[leaves_1] < partial[leaves_1 + 1])
leaves_1 ++;
if (partial[leaves_1]>partial[0])
{
partial[Root] = partial[leaves_1];
Root = leaves_1;
leaves_1 *= 2;
}
else NO_Ok= false;
}
partial[Root] = partial[0];
}
void BUildHEap(int*partial,int N)
{
int Root = N / 2;
while (Root > 0)
{
rebuild_heap(partial, N, Root);
Root--;
}
}
void RebuildHeapsort(int*partial, int*initial, int N, int squenseIncIndex)
{
int index=N;
sort(initial + 1, initial+N + 1, RBCMP);
while (initial[index] == partial[index])index--;
if (index==N)
{
BUildHEap(partial, N);
}
else if (index>1)
{
partial[0]=partial[index];
partial[index] = partial[1];
partial[1] = partial[0];
rebuild_heap(partial,index-1,1);
}
}
void RBprintf(int *partial,int N)
{
for (int index = 1; index < N; index++)
cout << partial[index] << " ";
cout << partial[N] << endl;
}
int main()
{
int *initial;
int*partial;
int N,index;
int squenseIncIndex;
cin >> N;
initial = new int[N+1];
partial = new int[N + 1];
readlninitial(initial, N);
if (readln_IsInsert(initial, N, partial, &squenseIncIndex))
{
cout << "Insertion Sort" << endl;
if (squenseIncIndex < N)
InsertSort(partial, squenseIncIndex);
}
else
{
cout << "Heap Sort" << endl;
RebuildHeapsort(partial, initial, N, squenseIncIndex);
}
RBprintf(partial,N);
delete[]partial;
delete[]initial;
system("pause");
return 0;
}