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. ogg:Extract 进程遇长事务执行 Forcestop 引发的惨案

    http://www.linuxidc.com/Linux/2015-04/115777.htm SQL> select t.addr,t.START_DATE from v$transacti ...

  2. PHP深浅拷贝

    举个栗子: <?php class Example1 { public $name; public function __construct($name) { $this->name = ...

  3. php sprintf格式化注入

    URL:http://efa4e2c2b8df4ce69454639f4e3727071652c31167f341a4.game.ichunqiu.com/ 简单的说就是sprintf中%1$\'会将 ...

  4. 不相交集ADT--链表实现

    每一个集合用用一个链表来表示.链表的第一个对象作为它所在集合的代表.链表中每个对象都包含一个集合成员,一个指向下一个对象的指针,以及指向代表的指针.每个链表含head和tail指针,head指向链表的 ...

  5. 利用json模块解析dict报错找不到attribute 'dumps'[python2.7]

    [背景] 环境: RHEL 7.3 版本: python2.7 [错误情况] 写了一个简单的python脚本 将dict转换为json 脚本如下: #!/usr/bin/python #-*- cod ...

  6. caffe源码整个训练过程

    Caffe源码 Blob protected: shared_ptr<SyncedMemory> data_; shared_ptr<SyncedMemory> diff_; ...

  7. 2018-2019-2 网络对抗技术 20165301 Exp6 信息搜集与漏洞扫描

    2018-2019-2 网络对抗技术 20165301 Exp6 信息搜集与漏洞扫描 1.实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 2.实践内容 (1)各种搜索技巧的应用 (2)DNS ...

  8. MySQL的表管理

    首先,先选择数据库(极其特别重要,如果不选择,将默认为第一个数据库) mysql > use db_name; 查看所有表 mysql > show tables; 1.创建表 creat ...

  9. git团队开发

    用git有一年了,下面是我这一年来的git使用总结,覆盖了日常使用中绝大多数的场景.嗯,至少是够用一年了,整理出来分享给大家,不明白的地方可以回复交流. git设置关闭自动换行 git config ...

  10. Windows Azure 初体验

    最近看到windows azure 在做活动,只需花一块钱就可以体验一个月的windows azure. 于是,我就注册了一个账号也尝试一把云时代,传送门. 注册很简单的,成功后可以看到这个界面. 然 ...