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 ...
随机推荐
- November 05th, 2017 Week 45th Sunday
Do not pray for an easy life, pray for the strength to endure a difficult one. 不要祈求安逸的人生,祈求拥有撑过艰难的力量 ...
- BZOJ3673/3674:可持久化并查集
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
- Linq EF 添加数据执行事务处理
在EF4.1的DBContext中实现事务处理(BeginTransaction)和直接执行SQL语句的示例 2012-12-12 10:39 5538人阅读 ...
- JDBC中的SPI实现
DriverManger加载时候会调用如下方法: 关键代码:java.sql.DriverManager#loadInitialDrivers 然后 有时间整理一下
- Linux chmod +755和chmod +777 各是什么意思呢?
你可以在linux终端先输入ls -al,可以看到如: -rwx-r--r-- (一共10个参数)第一个跟参数跟chmod无关,先不管.2-4参数:属于user5-7参数:属于group8-10参数: ...
- day2-作业及答案
作业:第一组: 1.接收用户输入一个年份,判断是否是闰年(判断闰年的方法是该年能被4整除并且不能被100整除,或者是可以被400整除) 2.接收用户输入一组整数,输入负数时结束输入,输出这组数字的和: ...
- 数据同步canal服务端HA配置
canal服务端HA模式,本人并未使用过,为保证文章的完整性,从以下地址摘抄该部分内容,待以后验证及使用 https://github.com/alibaba/canal/wiki/AdminGuid ...
- leetcode 217—Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- .NET批量操作窗口样式
1. 背景 我们在开发过程中,可能会遇到需要批量控制程序中窗体的大小或其它一些操作, 这些窗体有可能是属于程序本身的,也许是其它程序的窗口.本文就是基于此的一篇关于如何批量操作窗口样式的,我们主要是通 ...
- jq中each的中断
最近在做项目中,遇到jq的each方法中的回调函数里面的break不生效,即通过 jquery 的循环方法进行数组遍历,但是当不符合条件时,怎么跳出当前循环,我们经常会习惯JS中的break和cont ...