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内核同步原语之原子操作
避免对同一数据的并发访问(通常由中断.对称多处理器.内核抢占等引起)称为同步. ——题记 内核源码:Linux-2.6.38.8.tar.bz2 目标平台:ARM体系结构 原子操作确保对同一数据的“读 ...
- Term Term ssh登陆linux后 显示乱码
setup----terminal----locale----“chinese” OK!!!!!
- NoSQL-来自维基百科
NoSQL有时也称作Not Only SQL的缩写,是对不同于传统的关系型数据库的数据库管理系统的统称. 两者存在许多显著的不同点,其中最重要的是NoSQL不使用SQL作为查询语言.其数据存储可以不需 ...
- 26_Python的内置函数
The Python interpreter has a number of functions and types built into it that are always available.P ...
- ssh使两台机器建立连接
ssh利用口令建立连接过程: 客户端--> 发送连接请求 --> 远程主机 --> 返回远程主机的公钥 --> 公钥加密客户端私钥+客户端公钥返回远程主机 --> 远程主 ...
- EnumSet基本用法
enum Season { SPRING, SUMMER, FALL, WINTER } public class EnumSetTest { public static void main(Stri ...
- Selenium_Grid
Selenium Grid 工作原理 Grid是一种分布式测试工具,整个结果由一个hub主节点和若干个node代理节点组成. hub用来管理各个代理节点的注册和状态信息,并且接收远程客户端代码请求调用 ...
- xpath简单应用
相对路径与绝对路径: 如果"/"处在XPath表达式开头则表示文档根元素,(表达式中间作为分隔符用以分割每一个步进表达式)如:/messages/message/subject是一 ...
- 关于Android不同系统版本的市场占比情况详解
一,google官方统计的不同Android版本市场的占比强开 google统计的数据情况 这个是google官方对于不同版本的市场占比情况.这个是针对全世界所有的Android手机占比情况. 二,友 ...
- 超简教程:Xgboost在Window上的安装(免编译)
Xboost在windows安装需要自己编译,编译的过程比较麻烦,而且需要复杂的软件环境.为了免去编译,我这里把编译好的文件上传到网盘供大家下载安装.有了编译好的文件,xgboost的安装变得超级简单 ...