PAT1089. Insert or Merge

题目大意

给定一个初始序列src, 一个排序当中的序列tar, 问排序方式是 Insert Sort, 或者 Merge Sort. 并输出下一次迭代排序的序列, 保证答案唯一.

思路

由于保证了答案的唯一性, 所以先检测是否是 Insert Sort, 只要检测第一个出现不递增的序列的位置之后, src 和 tar 是否相等.

如果是 Merge Sort, (需要注意此处的 Merge Sort 不是二分实现的), 需要自己手动模拟 Merge Sort 的过程, 这里考察一个 按照 k 的大小分割一个区间

	for(int i = 0; i < n / k; i++)
func(i + k, (i + 1) * k);
func(n / k * k, n);

代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int nNum;
vector<int> src, tar;
bool eq(){
for(int i = 0; i < nNum; i++)
if(src[i] != tar[i])
return false;
return true;
}
int main(){
scanf("%d", &nNum);
src.resize(nNum); tar.resize(nNum);
int i, j;
for(i = 0; i < nNum; i++) cin >> src[i];
for(i = 0; i < nNum; i++) cin >> tar[i]; for(i = 0; i < nNum - 1 && tar[i] <= tar[i + 1]; i++);
for(j = i + 1; j < nNum && tar[j] == src[j]; j++); if(j == nNum){
cout << "Insertion Sort" << endl;
sort(src.begin(), src.begin() + i + 2);
}
else{
cout << "Merge Sort" << endl;
int k = 1, flag = 1;
while(flag){
flag = !eq();
k *= 2;
for(int i = 0; i < nNum / k; i++)
sort(src.begin() + i * k, src.begin() + (i + 1) * k);
sort(src.begin() + nNum / k * k, src.end());
}
}
for(int i = 0; i < nNum; i++){
printf("%s%d", i == 0 ? "" : " ", src[i]);
}
return 0;
}

PAT1089. Insert or Merge的更多相关文章

  1. pat1089. Insert or Merge (25)

    1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Accor ...

  2. PAT甲级1089. Insert or Merge

    PAT甲级1089. Insert or Merge 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到 ...

  3. PTA Insert or Merge

    According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and gr ...

  4. 60. Insert Interval && Merge Intervals

    Insert Interval Given a set of non-overlapping intervals, insert a new interval into the intervals ( ...

  5. 【题解】【区间】【二分查找】【Leetcode】Insert Interval & Merge Intervals

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

  6. PAT 1089 Insert or Merge[难]

    1089 Insert or Merge (25 分) According to Wikipedia: Insertion sort iterates, consuming one input ele ...

  7. PTA 09-排序2 Insert or Merge (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/675 5-13 Insert or Merge   (25分) According to ...

  8. Insert or Merge

    7-13 Insert or Merge(25 分) According to Wikipedia: Insertion sort iterates, consuming one input elem ...

  9. PAT_A1089#Insert or Merge

    Source: PAT A1089 Insert or Merge (25 分) Description: According to Wikipedia: Insertion sort iterate ...

随机推荐

  1. 腾讯毛华:智能交互,AI助力下的新生态

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 演讲人:毛华 腾讯云语音云总经理 背景:5月23-24日,以"焕启"为主题的腾讯"云+未来"峰会在广 ...

  2. 2018上半年DDoS攻击报告:流量峰值达1.7Tbps

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 2018年上半年DDoS攻防仍如火如荼发展,以IoT设备为反射点的SSDP反射放大尚未平息,Memcached DDoS又异军突起,以最高可 ...

  3. Oracle 过程中变量赋值

    create or replace function get_sal1(id employees.employee_id%type) return number is sal employees.sa ...

  4. 循环结构 while

    while 循环语句可以根据某些条件重复执行一条t-sql 语句或一个语句块 语法: while (条件) begin 语句或语句块 end 程序调试 alt+f5 启动调试 f9 切换断点  f10 ...

  5. 合约实战,代币合约,DAPP开发

    1. ERC20标准 https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md pragma solidity ^; //定义接口 con ...

  6. Spring MVC+Fastjson之时间类型序列化

    时间类型序列化: 注意红色代码,必须引入fastjson的JSONField类,而非其它. import com.alibaba.fastjson.annotation.JSONField; @Ent ...

  7. java常用API之System类

    System中代表程序所在系统,提供了对应的一些系统属性信息,和系统操作.System类不能手动创建对象,因为构造方法被private修饰,阻止外界创建对象.System类中的都是static方法,类 ...

  8. JavaScript写的随机选人真实案例

    JavaScript写的随机选人真实案例 因工作需要,写了一个随机选人的小网页,先看效果图. 背景也是动态的,只不过在写的时候碰到个问题,就是如果把生成动态流星雨的画布放到上生成随机数的操作界面之上的 ...

  9. vbScript: 编号成生不夠位數前面加零

    '编号成生Geovin Du '不夠位數前面加零 塗聚文 Function getStringlen(str,lenint) so="" itop=0 slen=Len(str) ...

  10. Node 的fs模块

    这个fs.readdir路径要加上__dirname  找到绝对路径  否则会报错 { Error: ENOENT: no such file or directory, scandir '/User ...