PAT----A1089 && A1098

A1089 && A1098

题意

给出两个序列,第二个序列是经过若干步排序的结果,判断是那种排序,并输出下一步排序的结果。重点要注意一定是要经过至少一步排序才判断是否和第二个序列相等,否则会有一个测试点通不过。

思路
总结

这两个提莫比较相像,放在这里总结一下。

A1089 Insert or Merge (25point(s))

Sample Input1:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

Sample Output1:

Insertion Sort
1 2 3 5 7 8 9 4 6 0

Sample Input2:

10
3 1 2 8 7 5 9 4 0 6
1 3 2 8 5 7 4 9 0 6

Sample Output2:

Merge Sort
1 2 3 8 4 5 7 9 0 6
#include"bits/stdc++.h"
using namespace std;
vector<int> a1,a2;
void show(vector<int>& a){
	for(int i=0;i<a.size();i++){
		if(i != a.size()-1)
			printf("%d ",a[i]);
		else
			printf("%d\n",a[i]);
	}
}
bool is_insertSort(){
	vector<int> at = a1 ;
	bool same = false;
	int n = a1.size();
	for(int i=2;i<=n;i++){
		same = at == a2 && i != 2;
		sort(at.begin(),at.begin()+i);	
		if(same){
			printf("Insertion Sort\n");
			show(at);
			return true;
		}
	}
	return false;
}
bool is_mergeSort(){
	vector<int> at = a1;
	int n = a1.size();
	bool same = false;
	int len = 2;
	while(len <= n){
		int l = 0;
		while(l < n){
			int r = min(n,l+len);
			sort(at.begin()+l,at.begin()+r);
			l += len;
		}
		len *= 2;
		if(at == a2){
			same = true;
			continue;
		}
		if(same){
			break;
		}
	}
	if(same){
		printf("Merge Sort\n");
		show(at);
		return true;
	}
	return false;
}
int main(){
	// freopen("input.txt","r",stdin);
	int n; cin >> n;
	a1.resize(n); a2.resize(n);
	for(int i=0;i<n;i++) scanf("%d",&a1[i]);
	for(int i=0;i<n;i++) scanf("%d",&a2[i]);
	if(is_insertSort() || is_mergeSort());
}

A1098 Insertion or Heap Sort (25point(s))

Sample Input1:

10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0

Sample Output1:

Insertion Sort
1 2 3 5 7 8 9 4 6 0

Sample Input2:

10
3 1 2 8 7 5 9 4 6 0
6 4 5 1 0 3 2 7 8 9

Sample Output2:

Heap Sort
5 4 3 1 0 2 6 7 8 9
#include"bits/stdc++.h"
using namespace std;
vector<int> a1,a2;
vector<int> temp;
void show(vector<int> &a,int low,int high) {
	for(int i=low; i<=high; i++) {
		if(i != high)
			printf("%d ",a[i]);
		else
			printf("%d\n",a[i]);
	}
}
bool is_insertSort() {
	for(int i=2; i<=temp.size(); i++) {
		bool same = false;
		if(temp == a2 && i!=2)
			same = true;
		sort(temp.begin(),temp.begin()+i);
		if(same) {
			printf("Insertion Sort\n");
			show(temp,0,temp.size()-1);
			return true;
		}
	}
	temp = a1; // reset
	return false;
}
void downAdjust(vector<int> &a,int low,int high) {
	int i = low;
	int j = i*2;
	while(j <= high) {
		if(j+1 <= high && a[j+1]>a[j])
			j ++;
		if(a[j] > a[i]) {
			swap(a[i],a[j]);
			i = j;
			j *= 2;
		} else
			break;
	}
}
bool is_heapSort() {
	vector<int> a(a1.size()+1);
	for(int i=1; i<=a1.size(); i++) {
		a[i] = a1[i-1];
	}
	int n = a1.size();
	for(int i=n/2; i>=1; i--) {
		downAdjust(a,i,n);
	}
	bool same = false;
	for(int i=n; i>=1; i--) {
		swap(a[i],a[1]);
		downAdjust(a,1,i-1);
		if(same) {
			printf("Heap Sort\n");
			show(a,1,n);
			return true;
		}
		same = true;
		for(int j=1; j<=n; j++) {
			if(a[j] != a2[j-1]) {
				same = false;
				break;
			}
		}
	}
	return false;
}
int main() {
	// freopen("input.txt","r",stdin);
	int n;  cin >> n;
	a1.resize(n);  a2.resize(n);
	for(int i=0; i<n; i++) scanf("%d",&a1[i]);
	for(int i=0; i<n; i++) scanf("%d",&a2[i]);
	temp = a1;
	if(is_insertSort() || is_heapSort());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值