1098. Insertion or Heap Sort

原创 2015年11月17日 21:05:24

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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

PAT(Advance) 1098. Insertion or Heap Sort (25)

#include #include #include using namespace std; vector orignal,target,result;bool InsertSort(){ ...
  • andyyang0212
  • andyyang0212
  • 2015年05月29日 19:24
  • 678

PAT A 1098. Insertion or Heap Sort (25)

题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, a...
  • xyzchenzd
  • xyzchenzd
  • 2015年04月07日 11:03
  • 1808

1098. Insertion or Heap Sort (25)-PAT甲级真题(堆排序)

1098. Insertion or Heap Sort (25) According to Wikipedia: Insertion sort iterates, consuming one...
  • liuchuo
  • liuchuo
  • 2016年08月19日 16:31
  • 398

5-1 Insertion or Heap Sort (9分)

堆排序和插入排序之间的区别程序题
  • csucbMIT
  • csucbMIT
  • 2016年12月14日 13:37
  • 517

Bubble, Insertion, Selection, Quick and Heap Sort

Bubble, Insertion, Selection, Quick and Heap Sort
  • guoxiaojie_415
  • guoxiaojie_415
  • 2015年08月15日 15:56
  • 869

5-1 Insertion or Heap Sort   (9分)

5-1 Insertion or Heap Sort   (9分) According to Wikipedia: Insertion sort iterates, consuming o...
  • yhf_naive
  • yhf_naive
  • 2016年12月17日 22:34
  • 502

PAT 1098. Insertion or Heap Sort

According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and g...
  • solin205
  • solin205
  • 2015年03月23日 16:16
  • 372

【PAT】1098. Insertion or Heap Sort

考查点:堆排序,插入排序 思路:用插入排序判断,直接用sort方法 提交情况:这里由于调用insert函数过程中heap已经改变所以在堆排序时必须复原heap,还有用vector必须从1开始所以初...
  • KID_LWC
  • KID_LWC
  • 2017年02月14日 00:50
  • 137

1098. Insertion or Heap Sort (25)

输入N接下来有两行 一行原始无序的 第二行在两种排序过程的某一步结果; 求再做一步的结果是甚么; Insert sort 直接插入排序,当N>1时;进行N-1循环,每次的主角为2~N; ...
  • u014646950
  • u014646950
  • 2015年08月03日 12:34
  • 682

1098. Insertion or Heap Sort 解析

和前面的 1088一样的,不过这次是堆排序。 堆排的时候前面是个最大堆,取最大值后调整堆就好。 #include #include #define MAX 110 using namesp...
  • sheepyWYY
  • sheepyWYY
  • 2017年05月05日 17:25
  • 167
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:1098. Insertion or Heap Sort
举报原因:
原因补充:

(最多只允许输入30个字)