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 ...
随机推荐
- 利用MSF的MS08_067模块攻击windows server 2003 SP2中文版系统
一.测试环境 攻击机:kali(NMAP+MSF) 靶机:windows server 2003 SP2 中文版 利用漏洞:MS08_067 二.漏洞描述 MS08-067漏洞的全称为“Windows ...
- PyQt5--MainWindow
# -*- coding:utf-8 -*- ''' Created on Sep 14, 2018 @author: SaShuangYiBing ''' import sys from PyQt5 ...
- 解决The valid characters are defined in RFC 7230 and RFC 3986错误问题
分析原因: 导致上述问题是因为tomcat自tomcat 8.0.35版本之后对URL参数做了比较规范的限制,必须遵循RFC 7230 and RFC 3986规范,对于非保留字字符(json格式的请 ...
- 函数式编程的终极形式:面向映射流的编程pipeline
1.单体(数据)映射:基本操作:数据的单次映射: 2.管道流:数据的流程化处理 基础是monand类型,形式是声明式编程: Pipeline模型: 它以一种“链式模型”来串接不同的程序或者不同的组件, ...
- BZOJ3473:字符串(后缀数组,主席树,二分,ST表)
Description 给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串? Input 第一行两个整数n,k. 接下来n行每行一个字符串. Output 一 ...
- JSSDK图像接口多张图片上传下载并将图片流写入本地
<span style="font-size: 14px;"><!DOCTYPE html> <html lang="en"> ...
- luogu P1891 疯狂LCM
嘟嘟嘟 这题跟上一道题有点像,但是我还是没推出来--菜啊 \[\begin{align*} ans &= \sum_{i = 1} ^ {n} \frac{i * n}{gcd(i, n)} ...
- Linux命令——磁盘管理
Linux命令--磁盘管理 命令df 作用:查看已挂载磁盘的总容量.使用容量.剩余容量等 参数:-i,查看inodes的使用状况 参数:-h,使用合适的单位显示(推荐) 命令du 作用:查看某个目录或 ...
- css 文本溢出
多行文本溢出处理: display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 3; // 3 行 overflow ...
- WorldWind源码剖析系列:窗口定制控件类WorldWindow
在WorldWindow定制控件是从Control类派生出来的,需要自己操纵GDI+绘制所需要的界面效果,这种自定义控件比较耗费精力,需要比较深厚的GDI+和DirectX 3D开发功底.(区别于用户 ...