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 ...
随机推荐
- Apache的权限设置与构建虚拟web主机
实验拓扑图: 实验要求: 1. 搭建WEB服务器,能访问默认站点,并使用awstats软件能监控到默认站点的访问情况. 2. 修改Apache的主配置文件,设置1.10只能访问awstats网站, ...
- [2018HN省队集训D9T1] circle
[2018HN省队集训D9T1] circle 题意 给定一个 \(n\) 个点的竞赛图并在其中钦定了 \(k\) 个点, 数据保证删去钦定的 \(k\) 个点后这个图没有环. 问在不删去钦定的这 \ ...
- 2018.09.01 09:08 Genesis
Nothing to think about, I don't know where to start, the mastery of learning is not an easy task, yo ...
- [Python2]介绍关于Uiautomator的watcher使用场景及使用方法
[官方的介绍]: Watcher You can register watcher to perform some actions when a selector can not find a mat ...
- Spark项目之电商用户行为分析大数据平台之(十)IDEA项目搭建及工具类介绍
一.创建Maven项目 创建项目,名称为LogAnalysis 二.常用工具类 2.1 配置管理组建 ConfigurationManager.java import java.io.InputStr ...
- linux服务器关闭ipv6 方法
第一个文件: /etc/sysconfig/network 第二个文件:如无此文件,vim添加 /etc/modprobe.d/disable_ipv6.conf
- leetcode121—Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- input输入框的光标定位的问题
input输入框的光标定位的问题 在给input输入框赋值的时候,或者在我之前写模拟下拉框js组件的时候,时不时会碰到光标的小bug问题,比如键盘中的 上移或者下移操作,在浏览器中光标会先移到最前端, ...
- neo4j用collect 代替union 并实行分页
MATCH pa=(j:User)-[r:PostLikeRel|:ReplyRel|:RetweetRel]->(m:User{guid:"f092a1dc6c23b26b020bd ...
- day75
昨日回顾: 1 inclusion_tag -干什么用的?生成html的片段(动态,传参数,传数据) -app下新建一个模块,templatetags -创建一个py文件(mytag.p ...