PAT甲级1089. Insert or Merge

题意:

根据维基百科:

插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表。每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到它所属的位置,并将其插入到该列表中。它重复,直到没有输入元素保留。

合并排序工作如下:将未排序的列表分为N个子列表,每个子列表包含1个元素(1个元素的列表被视为排序)。然后重复合并两个相邻的子列表以生成新的排序子列表,直到只剩下1个子列表。

现在给出整数的初始序列,

连同一些序列,这是一些排序方法的几次迭代的结果,你能告诉我们使用哪种排序方法吗?

输入规格:

每个输入文件包含一个测试用例。对于每种情况,第一行给出正整数N(<= 100)。然后在下一行中,给出N个整数作为初始序列。

最后一行包含N个数的部分排序顺序。假设目标序列总是上升。一行中的所有数字都以空格分隔。

输出规格:

对于每个测试用例,请在第一行打印“插入排序”或“合并排序”以指示用于获取部分结果的方法。

然后运行此方法再一次迭代,并在第二行输出结果序列。确保每个测试用例的答案是唯一的。一行中的所有数字必须用一个空格分开,并且行尾不能有额外的空格。

思路:

找到第一个逆序的地方,理论上,如果是插入,这个逆序的地方往后都不会有变化。归并反之。

插入很好处理。归并的话要找到此时的interval即可。

ac代码:

C++

// pat1089.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<map>
#include<cmath>
#include<unordered_map>
#include<unordered_set> using namespace std; int main()
{
int n;
scanf("%d", &n);
vector<int> list1(n);
vector<int> list2(n); for (int i = 0; i < n; i++)
{
scanf("%d", &list1[i]);
}
for (int i = 0; i < n; i++)
{
scanf("%d", &list2[i]);
} int pos = 0;
for (int i = 1; i < n; i++)
{
if (list2[i] < list2[i - 1])
{
pos = i;
break;
}
}
bool flag = true;
for (int i = pos; i < n; i++)
{
if (list1[i] != list2[i])
{
flag = false;
break;
}
} if (flag)
{
printf("Insertion Sort\n");
sort(list2.begin(), list2.begin() + pos + 1);
for (int i = 0; i < n - 1; i++)
printf("%d ", list2[i]);
printf("%d\n", list2[n - 1]);
}
else
{
printf("Merge Sort\n");
int interval = pos;
for (int i = pos; i >= 1; i--)
{
flag = true;
for (int j = 0; j * i < n; j++)
{
for (int u = i * j + 1; u < i * (j + 1) && u < n; u++)
{
if (list2[u] < list2[u - 1])
{
flag = false;
break;
}
}
if (!flag) break;
}
if (flag)
{
interval = i;
break;
}
} interval *= 2;
for (int i = 0; i * interval < n; i++)
{
if((i + 1) * interval < n)
sort(list2.begin() + i * interval, list2.begin() + (i + 1) * interval);
else
sort(list2.begin() + i * interval, list2.end());
} for (int i = 0; i < n - 1; i++)
printf("%d ", list2[i]);
printf("%d\n", list2[n - 1]);
}
return 0;
}

PAT甲级1089. Insert or Merge的更多相关文章

  1. PAT甲级——A1089 Insert or Merge

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  2. PAT Advanced 1089 Insert or Merge (25) [two pointers]

    题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...

  3. PAT甲级:1089 Insert or Merge (25分)

    PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...

  4. PAT 1089 Insert or Merge[难]

    1089 Insert or Merge (25 分) According to Wikipedia: Insertion sort iterates, consuming one input ele ...

  5. 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)

    题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...

  6. PAT 1089. Insert or Merge (25)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  7. PAT (Advanced Level) 1089. Insert or Merge (25)

    简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...

  8. PAT 1089. Insert or Merge

    Insertion sort iterates, consuming one input element each repetition, and growing a sorted output li ...

  9. 1089 Insert or Merge(25 分)

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

随机推荐

  1. UIAutomation Diagram

  2. JSON简介——(0)

    JSON: JavaScript Object Notation(JavaScript 对象表示法) JSON 是存储和交换文本信息的语法.类似 XML. JSON 比 XML 更小.更快,更易解析. ...

  3. C++学习之路(十):虚继承引入的执行效率

    这篇文章不知道取啥名字了,暂且这样叫,直接看场景就明白了.节选自<深度探索C++对象模型> Point3d origin, *pt = &origin; (1)origin.x = ...

  4. React Native 快速入门之认识Props和State

    眼下React Native(以后简称RN)越来越火,我也要投入到学习当中.对于一个前端来说,还是有些难度.因为本人觉得这是一个App开发的领域,自然是不同.编写本文的时候,RN的版本为0.21.0. ...

  5. 当array_filter函数的callback留空时 他会过滤掉所有键值为false的键

    当array_filter函数的callback留空时 他会过滤掉所有键值为false的键

  6. mysql cursor游标的使用,实例

    mysql被oracle收购后,从mysql-5.5开始,将InnoDB作为默认存储引擎,是一次比较重大的突破.InnoDB作为支持事务的存储引擎,拥有相关的RDBMS特性:包括ACID事务支持,数据 ...

  7. javascript当中的this详解

    总结this的3个规则: this是调用上下文,上下文被创建或者初始化时才确定 非严格模式:this是全局对象:严格模式:this是undefined 函数调用 a. 以函数形式调用的函数通常不使用t ...

  8. LightOJ - 1179 Josephus Problem(约瑟夫环)

    题目链接:https://vjudge.net/contest/28079#problem/G 题目大意:约瑟夫环问题,给你n和k(分别代表总人数和每次要数到k),求最后一个人的位置. 解题思路:因为 ...

  9. Porting of cURL to Android OS using NDK (from The Software Rogue)

    Porting of cURL to Android OS using NDK   In continuing my journey into Android territory, I decided ...

  10. 【hdoj_1133】Buy the Ticket(卡特兰数+大数)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1133 题目的意思是,m个人只有50元钱,n个人只有100元整钱,票价50元/人.现在售票厅没钱,只有50元 ...