1089. Insert or Merge (25)-判断插入排序还是归并排序
判断插入排序很好判断,不是的话那就是归并排序了。
由于归并排序区间是2、4、8开始递增的,所以要判断给出的归并排序执行到哪一步,就要k从2开始枚举。
然后再对每个子区间进行一下sort即可。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
const int maxn=;
int num1[maxn],num2[maxn]; int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&num1[i]);
for(int i=;i<n;i++)
scanf("%d",&num2[i]);
int p,idx;
for(idx=;num2[idx]<=num2[idx+];idx++);
p=idx+;
for(;num2[p]==num1[p] && p<n;p++);
//printf("idx:%d p:%d\n",idx,p);
if(p==n){
sort(num2,num2+idx+);
printf("Insertion Sort\n");
printf("%d",num2[]);
for(int i=;i<n;i++){
printf(" %d",num2[i]);
}
}
else{
int k;
bool flag=true;
for(k=;k<=n;k=(k<<)){
for(int i=;i<n;i+=k){
for(int j=i;j<i+k-&&j<n-;j++){
if(num2[j]>num2[j+]){
flag=false;
break;
}
}
if(!flag)
break;
}
if(!flag)
break;
}
for(int i=;i<n;i+=k){
if(i+k-<n){
sort(num2+i,num2+i+k);
}
else
sort(num2+i,num2+n);
}
printf("Merge Sort\n");
printf("%d",num2[]);
for(int i=;i<n;i++){
printf(" %d",num2[i]);
}
}
return ;
}
1089. Insert or Merge (25)-判断插入排序还是归并排序的更多相关文章
- PAT甲级:1089 Insert or Merge (25分)
PAT甲级:1089 Insert or Merge (25分) 题干 According to Wikipedia: Insertion sort iterates, consuming one i ...
- 1089. Insert or Merge (25)
题目如下: According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, ...
- 【PAT甲级】1089 Insert or Merge (25 分)(插入排序和归并排序)
题意: 输入一个正整数N(<=100),接着输入两行N个整数,第一行表示初始序列,第二行表示经过一定程度的排序后的序列.输出这个序列是由插入排序或者归并排序得到的,并且下一行输出经过再一次排序操 ...
- 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)
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& ...
- 1089 Insert or Merge (25 分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- 1089 Insert or Merge (25分)
According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...
- pat1089. Insert or Merge (25)
1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Accor ...
随机推荐
- .Net使用163smtp发送邮件时错误:邮箱不可用. has no permission解决方法
C#实现简单邮件发送代码如下 public static void SendAsync(string emailTo, string subject, string mailBody) { var m ...
- git版本管理工具-git的概述
什么是git Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目的一种工具 Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不 ...
- 1、pyspider安装
系统环境: centos6.6.python2.7 经测试,python2.6安装的pyspider会导致webui无法正常访问 参考博文: http://cuiqingcai.com/2443.ht ...
- Python2.7-random
random 模块,实现了各种分布下的伪随机数生成器.对于整数,可以从一个范围中随机挑选:对于序列,可以随机挑选其中的元素但不改变原序列,也可以对序列中的元素进行重新排列.此外,模块还封装了各种分布函 ...
- torchvision
torchvision是一个包,它服务于pytorch深度学习框架,用来生成图片,视频数据集,模型类和预训练的模型torchvision由以下四个部分组成:1. torchvision.dataset ...
- Android 自定义底部公用菜单
注释:此案例主要展示自定义底部菜单,一处封装处处调用.使用起来相当方便 一.初始的Activity package com.example.myapi.buttommenu; import andro ...
- nginx rewrite flag
1.break可以理解为switch中的break,而last可以理解为continue,一个是跳出server{}的匹配规则,一个还将继续匹配之后的规则. 无论使用last还是break,浏览器上面 ...
- OpenGL笔记(五) 着色器渲染(以Android为例)
一.Android平台上下文环境的创建及初始化 1. 首先实例化Android上下文环境,即EGL的初始化. bool EGLCore::init(EGLContext sharedContext) ...
- 基于Python自动发送QQ群消息
1.准备工作 此次测试基于python3,需要安装qqbot.bs4.requests库. qqbot项目地址:https://github.com/pandolia/qqbot.git pip qq ...
- win10触摸板手势
尴尬的发现,今天才开始使用win10的手势,之前都是单指操作/笑哭 参考:http://www.sohu.com/a/63678223_230077 https://support.microsoft ...