PAT A1089 Insert or Merge
Sample Input 1:
10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0
Sample Output 1:
Insertion Sort
1 2 3 5 7 8 9 4 6 0
Sample Input 2:
10
3 1 2 8 7 5 9 4 0 6
1 3 2 8 5 7 4 9 0 6
Sample Output 2:
Merge Sort
1 2 3 8 4 5 7 9 0 6
word | meaning |
---|---|
iterates | [计]迭代 |
adjacent sublists | 相邻的子列表 |
ascending | adj. 上升的 |
-
思路 1:
先模拟插入排序,每轮迭代判断一次是否和目标相同,相同输出,若循环完依然不同,说明是归并排序,再模拟归并排序,知道和目标相同后再归并一次 -
code 1:狗屎!!
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
const int maxn = 111;
int n, before[maxn], after[maxn], tmp[maxn];
bool equal(){
for(int i = 0; i < n; ++i){
if(before[i] != after[i]) return false;
}
return true;
}
bool equal1(){
for(int i = 0; i < n; ++i){
if(tmp[i] != after[i]) return false;
}
return true;
}
int BinSearch(int left, int right, int x){
int mid;
while(left < right){
mid = (left + right) / 2;
if(before[mid] > x) right = mid;
else left = mid+1;
}
return left;
}
void insertSort(int i){
int pos = BinSearch(0, i, before[i]);
int tmp = before[i];
for(int j = i-1; j >= pos; --j){
before[j+1] = before[j];
}
before[pos] = tmp;
}
void emergeSort(int len){
int id = 0, lenth = len*2;
while(1){
if(id+lenth < n){
sort(tmp+id, tmp+id+lenth);
id += lenth;
}
else{
sort(tmp+id, tmp+n);
break;
}
}
}
int main(){
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d", &before[i]);
tmp[i] = before[i];
}
for(int i = 0; i < n; ++i){
scanf("%d", &after[i]);
}
bool flag = false;
for(int i = 1; i < n; ++i){
insertSort(i);
if(equal() == true){
printf("Insertion Sort\n");
insertSort(i+1);
for(int i = 0; i < n; ++i){
printf("%d", before[i]);
if(i < n-1) printf(" ");
}
flag = true;
break;
}
}
if(flag == false){
printf("Merge Sort\n");
for(int i = 1; i < n; i*=2){
emergeSort(i);
if(equal1() == true){
emergeSort(i*2);
for(int j = 0; j < n; ++j){
printf("%d", tmp[j]);
if(j < n-1) printf(" ");
}
break;
}
}
}
return 0;
}
- T2 code:
#include <bits/stdc++.h>
using namespace std;
vector<int> ori(110), tes(110), aim(110);
void InsertSort(int x){
tes[0] = tes[x]; //哨兵
for(int i = x; i >= 0; --i){
if(tes[i-1] > tes[0]){
tes[i] = tes[i-1];
}else{
tes[i] = tes[0];
return;
}
}
}
void Merge(int left, int mid, int right){
vector<int> tmp;
int i = left, j = mid;
while(i < mid && j < right){
if(tes[i] < tes[j]){
tmp.push_back(tes[i]);
i++;
}else{
tmp.push_back(tes[j]);
j++;
}
}
while(i < mid){
tmp.push_back(tes[i]);
i++;
}
while(j < right){
tmp.push_back(tes[j]);
j++;
}
for(int id = left; id < right; ++id){
int idex = id - left;
tes[id] = tmp[idex];
}
}
void MergeSort(int n, int x){
for(int i = 1; i <= n; i += 2*x){
int left = i, mid = i + x, right = i + 2 * x;
if(right > n + 1) right = n + 1;
Merge(left, mid, right);
}
}
bool Equal(int n){
for(int i = 1; i <= n; ++i){
if(tes[i] != aim[i]) return false;
}
return true;
}
void Print(int n){
for(int i = 1; i <= n; ++i){
printf("%d", tes[i]);
if(i < n) printf(" ");
}
}
int main(){
int n;
scanf("%d", &n);
for(int i = 0; i < 2; ++i){
for(int j = 1; j <= n; ++j){
if(i == 0){
scanf("%d", &ori[j]);
tes[j] = ori[j];
}else scanf("%d", &aim[j]);
}
}
bool not_insert = true;
for(int i = 2; i <= n; ++i){ //Wrong1:插入排序从第二个元素开始 样例2:-1
InsertSort(i);
if(Equal(n)){
not_insert = false;
printf("Insertion Sort\n");
InsertSort(i+1);
Print(n);
break;
}
}
if(not_insert){
tes = ori;
int x = 1;
while(x <= n){
MergeSort(n, x);
if(Equal(n)){
printf("Merge Sort\n");
MergeSort(n, 2 * x);
Print(n);
break;
}
x *= 2;
}
}
return 0;
}
- T3 code:
#include <bits/stdc++.h>
using namespace std;
vector<int> ori, aim;
bool InsertSort(vector<int> ori, vector<int> & aim, int n)
{
for(int i = 2; i < ori.size()-1; ++i)
{
sort(ori.begin(), ori.begin()+i);
if(ori == aim)
{
sort(aim.begin(), aim.begin()+i+1);
return true;
}
}
return false;
}
void Print(const vector<int> & v)
{
for(int i = 0; i < v.size(); ++i)
{
if(i == 0) printf("%d",v[i]);
else printf(" %d", v[i]);
}
printf("\n");
}
void Merge(vector<int> & v, int len, int n)
{
for(int i = 0; i < n; i += len)
{
int nex = i + len < n ? i + len : n;
sort(v.begin()+i, v.begin()+nex);
}
}
void MergeSort(vector<int> ori, vector<int> & aim, int n)
{
int len = 1;
while(len < n)
{
Merge(ori, len, n);
if(ori == aim)
{
Merge(aim, len<<1, n);
printf("Merge Sort\n");
Print(aim);
return;
}
len <<= 1;
}
}
int main()
{
int n;
scanf("%d", &n);
ori.resize(n); aim.resize(n);
for(int i = 0; i < n; ++i)
{
scanf("%d", &ori[i]);
}
for(int i = 0; i < n; ++i)
{
scanf("%d", &aim[i]);
}
if(InsertSort(ori, aim, n))
{
printf("Insertion Sort\n");
Print(aim);
}else
{
MergeSort(ori, aim, n);
}
return 0;
}
- T3 code:
#include <bits/stdc++.h>
using namespace std;
vector<int> ori, aim;
bool InsertSort(vector<int> ori, vector<int> & aim, int n)
{
for(int i = 2; i < ori.size()-1; ++i)
{
sort(ori.begin(), ori.begin()+i);
if(ori == aim)
{
sort(aim.begin(), aim.begin()+i+1);
return true;
}
}
return false;
}
void Print(const vector<int> & v)
{
for(int i = 0; i < v.size(); ++i)
{
if(i == 0) printf("%d",v[i]);
else printf(" %d", v[i]);
}
printf("\n");
}
void Merge(vector<int> & v, int len, int n)
{
vector<int> tmp;
for(int i = 0; i + len < n; i += 2 * len) //注意判断条件!!
{
int upper = i + 2 * len > n ? n : i + 2 * len; //这里出问题了!
int left = i, right = i + len;
while(left < i + len && right < upper)
{
if(v[left] < v[right]) tmp.push_back(v[left++]);
else tmp.push_back(v[right++]);
}
while(left < i + len) tmp.push_back(v[left++]);
while(right < upper) tmp.push_back(v[right++]);
}
for(int i = 0; i < tmp.size(); ++i)
{
v[i] = tmp[i];
}
}
void MergeSort(vector<int> & ori, vector<int> & aim, int n)
{
int len = 1;
while(len < n)
{
Merge(ori, len, n);
if(ori == aim)
{
Merge(aim, len<<1, n);
printf("Merge Sort\n");
Print(aim);
return;
}
len <<= 1;
}
}
int main()
{
int n;
scanf("%d", &n);
ori.resize(n); aim.resize(n);
for(int i = 0; i < n; ++i)
{
scanf("%d", &ori[i]);
}
for(int i = 0; i < n; ++i)
{
scanf("%d", &aim[i]);
}
if(InsertSort(ori, aim, n))
{
printf("Insertion Sort\n");
Print(aim);
}else
{
MergeSort(ori, aim, n);
}
return 0;
}
- T4 code:
#include <bits/stdc++.h>
using namespace std;
vector<int> aim;
void Print(const vector<int> & ans)
{
for(int i = 0; i < ans.size(); ++i)
{
printf("%d", ans[i]);
if(i < ans.size() - 1) printf(" ");
}
printf("\n");
}
void Insert(vector<int> & ori, int l, int r)
{
//在[l, r)范围内二分找插入位置LowerBound
int tmp = ori[r], idex = r;
while(l < r) //l == r时候: [l, l) 区间为空,退出
{
int mid = (l + r) / 2;
if(tmp < ori[mid])
{
r = mid;
}else
{
l = mid + 1;
}
}
for(int i = idex; i > l; --i)
{
ori[i] = ori[i-1];
}
ori[l] = tmp;
}
bool InsertSort(vector<int> ori)
{
for(int i = 1; i < ori.size(); ++i)
{
Insert(ori, 0, i);
if(ori == aim)
{
printf("Insertion Sort\n");
Insert(ori, 0, i+1);
Print(ori);
return true;
}
}
return false;
}
void Merge(vector<int> & ori, int L1, int R1, int L2, int R2)
{
//Merge: [L1, R1) 和 [L2, R2)
vector<int> tmp;
int idex = L1;
while(L1 < R1 && L2 < R2)
{
if(L2 < R2 && ori[L1] > ori[L2])
tmp.push_back(ori[L2++]);
if(L1 < R1 && ori[L1] < ori[L2])
tmp.push_back(ori[L1++]);
}
if(L1 == R1)
{
while(L2 < R2)
tmp.push_back(ori[L2++]);
}
if(L2 == R2)
{
while(L1 < R1)
tmp.push_back(ori[L1++]);
}
for(int i = 0; i < tmp.size(); ++i)
{
ori[idex + i] = tmp[i];
}
}
void MergeSort(vector<int> & ori)
{
bool flg = false;
for(int L = 1; L < ori.size(); L <<= 1)
{
for(int i = 0; i + L < ori.size(); i += 2 * L)
{
int r = i + 2 * L;
if(r > ori.size()) r = ori.size();
Merge(ori, i, i + L, i + L, r);
}
if(flg)
{
printf("Merge Sort\n");
Print(ori);
break;
}
if(ori == aim) flg = true;
}
}
int main()
{
int n;
scanf("%d", &n);
vector<int> ori(n); aim.resize(n);
for(int i = 0; i < n; ++i) scanf("%d", &ori[i]);
for(int i = 0; i < n; ++i) scanf("%d", &aim[i]);
if(!InsertSort(ori))
{
MergeSort(ori);
}
return 0;
}