1089. Insert or Merge (25)

102 篇文章 0 订阅
37 篇文章 0 订阅

1089. Insert or Merge (25)

时间限制
200 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.

Merge sort works as follows: Divide the unsorted list into N sublists, each containing 1 element (a list of 1 element is considered sorted). Then repeatedly merge two adjacent sublists to produce new sorted sublists until there is only 1 sublist remaining.

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 "Merge 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 resulting 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 0 6
1 3 2 8 5 7 4 9 0 6
Sample Output 2:
Merge Sort
1 2 3 8 4 5 7 9 0 6
输入:
N(个数总和)
原始数据队
排序到一半的队
要求回答是插入排序还是归并排序,并再给出对应操作的一步。
【直接插入Insert sort 】,当N>1时;进行N-1循环,每次的主角为2~N;
           每次把主角和与他前一个位置的配角比较,如果主角较小,把主角当前位置给与他比较的配角,并占据配角的位置,继续直到前面没有配角;
否则这一次结束
显然前面排好序的一定从小到达,后面还没有排好序的一定在原来的位置;
【归并排序】: 先两个分成一组排序;接着4个分成一组排序,再接着八个分成一组排序;……最后的N个排序;
          显然每组的个数要么2的幂要么为N;
我通过一次循环算每段从小到大排好的元素个数最少的【但不包括最后的那段】当作是大于或等于已经进行的markindex个分成一组;显然这个是不精确的,但是可以根据这个不精确的数获得下一步需要分成index一组排序(如果index>N,那么一次merge会自动调整为N);
不包括最后一段是 比如  1 2 5 6 3 4,显然最后一步可以直接就N了;
      1 2 3 7 8 9 5 6 4 一开是的markindex是6,但是需要调整为2;那么会获得接着的index是4
联动 Insertion or Heap Sort: http://xujiayu317.blog.163.com/blog/static/25475209201573115911922/

评测结果

时间结果得分题目语言用时(ms)内存(kB)用户
8月18日 20:06答案正确251089C++ (g++ 4.7.2)1308datrilla

测试点

测试点结果用时(ms)内存(kB)得分/满分
0答案正确12527/7
1答案正确11806/6
2答案正确13041/1
3答案正确13081/1
4答案正确11803/3
5答案正确13084/4
6答案正确13083/3
#include<iostream> 
#include<vector>
#include<algorithm>
using namespace std; 
bool readln(vector<int>*now, int N,int *markindex)
{
  int index;
  bool nodecrease= true;
  bool IsInsert = true;

  vector<int>sequence(N);
  for (index = 0; index < N; index++) 
    cin >> sequence[index]; 
  for (index = 0; index < N; index++)
  {
    cin >> (*now)[index]; 
    if (index>0)
    if (nodecrease&& (*now)[index] >=(*now)[index - 1])
      (*markindex) = index;
    else 
    {
      nodecrease = false;
      if (IsInsert&&(*now)[index] != sequence[index]) 
        IsInsert = false;  
    } 
  }  
  return IsInsert;
}
void Insertone(vector<int>*now, int markindex)
{   
  int temp =(*now) [markindex+1];
  while (markindex >= 0 && temp < (*now)[markindex])
  {
    (*now)[markindex+1] = (*now)[markindex]; 
    markindex--;
  }
  (*now)[markindex+1] = temp;
}
bool mergecmp(const int&A, const int &B){ return A < B; }
void mergesort(vector<int>*now, int N,int markindex)
{
  int index,temp; 
  markindex++;
  for (temp=index = markindex; index < N; index++)
  {
    if (temp<index&&(*now)[index] < (*now)[index-1])
    {
      if (index - temp < markindex&&index!=N-1)markindex = index - temp;
      temp = index;
    }
  }
  index = 1;
  while(index<=markindex)index *=2; 
  for (temp=0;temp<N;temp = markindex)
  {
    markindex = temp + index < N ? temp + index : N;
    sort((*now).begin()+temp, (*now).begin()+markindex,mergecmp); 
  }

}
void display(vector<int>*now, int N)
{
  cout << (*now)[0];
  for (int index = 1; index < N; index++)
    cout << " " << (*now)[index];
  cout << endl;
}
int main()
{ 
  int N,markindex=0;
  cin >> N;
  vector<int>now(N);
  if (readln( &now, N,&markindex))
  {  
    if (markindex+1<N)
    Insertone(&now,markindex); 
    cout << "Insertion Sort" << endl;
  }
  else
  { 
    mergesort(&now, N, markindex);
    cout << "Merge Sort" << endl;
  }
  display(&now, N);
  system("pause");
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值