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. C/C++——[02] 运算符和表达式

    C/C++中表示数据运算的符号称为“运算符”.运算符所用到的操作数个数,称为运算符的“目数”. C/C++语言的运算符有赋值运算符.算术运算符.逻辑运算符.位运算符等多类. 将变量.常量等用运算符连接 ...

  2. rpmdb: Thread/process 9180/139855524558592 failed: Thread died in Berkeley DB library

    使用yum安装出现问题:rpmdb: Thread/process 9180/139855524558592 failed: Thread died in Berkeley DB library 解决 ...

  3. Web服务器处理动态程序三种方式及Apache配置

    模块.CGI.FastCGI三种方式介绍 以PHP脚本为例: 模块方式是指Web服务器通过libphp5.so模块调用PHP服务,模块将相关函数嵌入Web服务请求处理流程,不需要额外解释器进程.注意, ...

  4. jQuery对的表单数据序列化和校验

    jQuery对的表单数据序列化和校验 表单序列化 如果想让表单通过ajax异步提交,那么首先我们要通过js获取到每个表单中输入的值,如果表单项比较多的话,是一件很麻烦,很痛苦的事情,那么我们可以通过j ...

  5. HTML 禁止显示input默认提示信息

    看问题 html代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  6. explicit浅谈

    在C++中,explicit关键字主要用于防止隐式转换,用于修饰构造函数.复制构造函数. 例如有一个类: class A { public: A( int count ) : m_data( coun ...

  7. day1 作业编写登录窗口

    作业一:编写登录接口 (1)输入用户名和密码: (2)认证成功后显示欢迎信息: (3)输错三次后锁定. 思路:我们知道,要想让程序记住之前输入多少次,锁定用户,那么可以使用数据库来保存用户的状态,然而 ...

  8. email 校验

    email 校验: javascript: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/ java: ^([a- ...

  9. C#后台获取ajax传来的xml格式数据值

    前台: var xml = "<root>"; if(Name!=null) { xml += "<name>"+Name +" ...

  10. 【WPF】设置Listview样式

    <ListView Grid.Row="0" ItemsSource="{Binding Lst_bind}"> <ListView.Item ...