PAT甲级1089. Insert or Merge
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的更多相关文章
- PAT甲级——A1089 Insert or Merge
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PAT Advanced 1089 Insert or Merge (25) [two pointers]
题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...
- PAT甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- PAT 1089 Insert or Merge[难]
1089 Insert or Merge (25 分) According to Wikipedia: Insertion sort iterates, consuming one input ele ...
- 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)
题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...
- PAT 1089. Insert or Merge (25)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- PAT (Advanced Level) 1089. Insert or Merge (25)
简单题.模拟一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...
- PAT 1089. Insert or Merge
Insertion sort iterates, consuming one input element each repetition, and growing a sorted output li ...
- 1089 Insert or Merge(25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
随机推荐
- Linux 上配置 NTP SERVER
在CENTOS 6.2上面安装配置NTP SERVER 安装NTP:yum install ntp 配置时间源vi /etc/ntp.confserver 210.72.145.44server nt ...
- 9.Python3标准库--数据压缩与归档
''' 尽管现代计算机系统的存储能力日益增长,但生成数据的增长是永无休止的. 无损(lossless)压缩算法以压缩或解压缩数据花费的时间来换取存储数据所需要的空间,以弥补存储能力的不足. Pytho ...
- javascript初步了解
0.1 <script> 和 </script> 会告诉 JavaScript 在何处开始和结束. <script> 和 </script> 之间的 ...
- request机制
每个框架中都有处理请求的机制(request),但是每个框架的处理方式和机制是不同的 为了了解Flask的request中都有什么东西,首先我们要写一个前后端的交互 基于HTML + Flask 写一 ...
- csu 1756(数论+去重)
Prime Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 84 Solved: 12[Submit][Status][Web Board] Descr ...
- 使用Python快速查询所有指定匹配KEY的办法
import redis redis_ip = '10.10.14.224' redis_port = 18890 # 配置redis的连接办法 # http://blog.csdn.net/u010 ...
- 禁用Flash P2P上传
Mac OS: sudo bash -c 'echo RTMFPP2PDisable=1 >> /Library/Application\ Support/Macromedia/mms.c ...
- centos7 开放端口号
firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --reload
- day4迭代器&生成器&正则表达式
一.迭代器 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不能后退,不过这也没什么,因为人们很少在迭代途中后退.另外,迭代器的一大优点 ...
- 【LOJ】 #2132. 「NOI2015」荷马史诗
题解 k叉哈夫曼树,但是没有了二叉那样的最后一定能合并成一个树根的优秀性质,我们就不断模拟操作看看到了哪一步能用的节点数< k,然后先拿这些节点数合并起来 然后就可以k个k个合并了,大小一样先拿 ...