1098. Insertion or Heap Sort (25)
2015-12-06 12:37
477 查看
make_heap 和 pop_heap 可以实现建堆的操作。
另外要注意插入排序下一次可能和本次相同,要输出直到不同的时候的序列。
时间限制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:
来源: <http://www.patest.cn/contests/pat-a-practise/1098>
[/code]
来自为知笔记(Wiz)
另外要注意插入排序下一次可能和本次相同,要输出直到不同的时候的序列。
时间限制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 0Sample Output 1:
Insertion Sort 1 2 3 5 7 8 9 4 6 0Sample Input 2:
10 3 1 2 8 7 5 9 4 6 0 6 4 5 1 0 3 2 7 8 9Sample Output 2:
Heap Sort 5 4 3 1 0 2 6 7 8 9
来源: <http://www.patest.cn/contests/pat-a-practise/1098>
#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#pragma warning(disable:4996)
using namespace std;
vector<int> init, patial, tempI, heapMerge, tempH, heap;
int main(void) {
freopen("Text.txt", "r", stdin);
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int itemp;
cin >> itemp;
init.push_back(itemp);
tempI.push_back(itemp);
tempH.push_back(itemp);
}
for (int i = 0; i < n; i++) {
int itemp;
cin >> itemp;
patial.push_back(itemp);
}
bool flag = false;
for (int i = 0; i <= n; i++) {
if (flag == true) {
cout << "Insertion Sort" << endl;
//sort(tempI.begin(), tempI.begin() + i + 1);
while (flag==true)
{
sort(tempI.begin(), tempI.begin() + i);
flag = true;
for (int j = 0; j < n; j++) {
if (tempI[j] != patial[j]) {
flag = false;
break;
}
}
i++;
}
for (int j = 0; j < n; j++) {
cout << tempI[j];
if (j != n - 1)
cout << " ";
}
return 0;
}
sort(tempI.begin(), tempI.begin() + i);
flag = true;
for (int j = 0; j < n; j++) {
if (tempI[j] != patial[j]) {
flag = false;
break;
}
}
}
/*for (int i = n; i >=0 ; i--) {
make_heap(tempH.begin(), tempH.begin() + i);
pop_heap(tempH.begin(), tempH.begin() + i);
bool flag = true;
for (int j = 0; j < n; j++) {
if (tempH[j] != patial[j]) {
flag = false;
break;
}
}
if (flag == true) {
make_heap(tempH.begin(), tempH.begin() + i-1);
pop_heap(tempH.begin(), tempH.begin() + i-1);
cout << "Heap Sort" << endl;
for (int j = 0; j < n; j++) {
cout << tempH[j];
if (j != n - 1)
cout << " ";
}
return 0;
}
}*/
int index=0;
bool f = false;
for (int i = n - 1; i > 0; i--) {
for (int j = i-1; j > 0; j--) {
if (f==false&&patial[j] > patial[i]) {
index = i + 1;
f = true;
break;
}
}
}
make_heap(patial.begin(), patial.begin() + index);
pop_heap(patial.begin(), patial.begin() + index);
cout << "Heap Sort" << endl;
for (int j = 0; j < n; j++) {
cout << patial[j];
if (j != n - 1)
cout << " ";
}
return 0;
}
[/code]
来自为知笔记(Wiz)
相关文章推荐
- org.apache.hadoop.hbase.ClockOutOfSyncException: org.apache.hadoop.hbase.ClockOutOfSyncException: Se
- UIDeviceOrientation
- 1097. Deduplication on a Linked List (25)
- [LeetCode]Implement Trie (Prefix Tree)
- 【思考一】Android程序员想做手机游戏开发
- 前后台切换会调用哪些方法
- 1096. Consecutive Factors (20)
- UEFI BIOS模式下Windows系统启动过程以及引导文件修复方法
- eclipse高效编程(一)
- 【Unix/Linux编程实践】 动手实现简单的more
- 软件开发概述
- Bete冲刺第二阶段
- G - sorry 没有北门
- 跨域问题:Access-Control-Allow-Origin
- 配置邮箱服务器
- A - 这是在网吧出的题!!
- 使用yum源安装CDH Hadoop集群
- 用Qemu模拟vexpress-a9 (一) --- 搭建Linux kernel调试环境
- 装饰设计模式(三)demo
- javaweb学习总结(七)——HttpServletResponse对象(一)