您的位置:首页 > 其它

1098. Insertion or Heap Sort

2015-11-17 21:05 375 查看


1098. Insertion or Heap Sort (25)

时间限制

100 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

According to Wikipedia:

Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts
it there. It repeats until no input elements remain.

Heap sort divides its input into a sorted and an unsorted region, and it iteratively shrinks the unsorted region by extracting the largest element and moving that to the sorted region. it involves the use of a heap data structure rather than
a linear-time search to find the maximum.

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

#include<iostream>
#include<vector>
#include<stack>
#include<stdio.h>
using namespace std;

int min(int a, int b)
{
return a > b ? b : a;
}

bool same(const vector<int> &v1, const vector<int> &v2)
{
for(int i = 0; i < v1.size(); i ++)
{
if(v1[i] != v2[i])
return false;
}
return true;
}

void maxHeapFixDown(vector<int> &original, int i, int n)
{
int j, tmp = original[i];
j = 2*i + 1;
while(j < n)
{
if(j+1 < n && original[j+1] > original[j])
j ++;
if(original[j] <= tmp)
break;
original[i] = original[j];
i = j;
j = 2*i + 1;
}
original[i] = tmp;
}

void maxHeap(vector<int> &original)
{
for(int i = original.size()/2 - 1; i >= 0; i --)
maxHeapFixDown(original, i, original.size());
}

vector<vector<int> >v1;//insertSort
vector<vector<int> >v2;//heapSort

void insertSort(vector<int> original)
{
int length = original.size();
for(int i = 1; i < length; i ++)
{
int j = i - 1;
while(j >= 0 && original[j] > original[i])
j --;
int tmp = original[i];

for(int k = i; k > j+1; k --)
original[k] = original[k-1];
original[j+1] = tmp;

if(v1.size() == 0 || !same(v1[v1.size()-1], original))
v1.push_back(original);
}
}

void heapSort(vector<int> original)
{
maxHeap(original);
for(int i = original.size()-1; i >= 1; i --)
{
swap(original[i], original[0]);
maxHeapFixDown(original, 0, i);
v2.push_back(original);
}
}

void print(const vector<int> &v)
{
for(int i = 0; i < v.size(); i ++)
if(i)
cout<<" "<<v[i];
else
cout<<v[i];
cout<<endl;
}

int main()
{
freopen("F://Temp/input.txt", "r", stdin);
int n;
cin>>n;

vector<int> input;
vector<int> ans;

for(int i = 0; i < n; i ++)
{
int num;
cin>>num;
input.push_back(num);
}

for(int i = 0; i < n; i ++)
{
int num;
cin>>num;
ans.push_back(num);
}

insertSort(input);
for(int i = 0; i < v1.size(); i ++)
{
if(same(v1[i], ans))
{
cout<<"Insertion Sort"<<endl;
if(i == v1.size()-1)
print(v1[i]);
else
print(v1[i+1]);

return 0;
}
}

heapSort(input);
for(int i = 0; i < v2.size(); i ++)
{
if(same(v2[i], ans))
{
cout<<"Heap Sort"<<endl;
if(i == v2.size()-1)
print(v2[i]);
else
print(v2[i+1]);

return 0;
}
}

return 0;
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: