PAT (Advanced Level) 1089. Insert or Merge (25)

简单题。模拟一下即可。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

const int maxn=200;
int a[maxn],b[maxn],n;

int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++) scanf("%d",&a[i]);
    for(int i=1; i<=n; i++) scanf("%d",&b[i]);

    int flag=1;

    int p=n+1;
    for(int i=2;i<=n;i++)
        if(b[i]<b[i-1]) {p=i; break;}

    for(int i=p;i<=n;i++)
    {
        if(a[i]==b[i]) continue;
        else flag=0;
    }

    if(flag==1)
    {
        printf("Insertion Sort\n");
        if(p==n+1) p=n;
        sort(a+1,a+p+1);
        for(int i=1; i<=p; i++)
        {
            printf("%d",a[i]);
            if(i<n) printf(" ");
            else printf("\n");
        }
        for(int i=p+1; i<=n; i++)
        {
            printf("%d",a[i]);
            if(i<n) printf(" ");
            else printf("\n");
        }
    }
    else
    {
        printf("Merge Sort\n");

        int len=1;
        while(1)
        {
            int pp=1;
            while(1)
            {
                if(pp+len-1>n)
                {
                    sort(a+pp,a+n+1);
                    break;
                }
                else
                {
                    sort(a+pp,a+pp+len);
                    pp=pp+len;
                }
            }

            int fail=0;
            for(int i=1; i<=n; i++) if(a[i]!=b[i]) fail=1;

            if(fail==0)
            {
                len=len*2;
                int ppp=1;
                while(1)
                {
                    if(ppp+len-1>n)
                    {
                        sort(a+ppp,a+n+1);
                        break;
                    }
                    else
                    {
                        sort(a+ppp,a+ppp+len);
                        ppp=ppp+len;
                    }
                }
                for(int i=1;i<=n;i++)
                {
                    printf("%d",a[i]);
                    if(i<n) printf(" ");
                    else printf("\n");
                }
                break;
            }
            len=len*2;
        }
    }

    return 0;
}
时间: 2024-10-24 06:50:35

PAT (Advanced Level) 1089. Insert or Merge (25)的相关文章

Pat(Advanced Level)Practice--1089(Insert or Merge )

Pat1089代码 题目描述: According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs with

1089. Insert or Merge (25)【排序】——PAT (Advanced Level) Practise

题目信息 1089. Insert or Merge (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, find

1089. Insert or Merge (25)

1089. Insert or Merge (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion s

PAT 1088 1089. Insert or Merge (25)(排序啊)

题目链接:http://www.patest.cn/contests/pat-a-practise/1089 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input d

PAT 1089. Insert or Merge (25)

According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted li

PAT Advanced Level 1013 Battle Over Cities (25)(25 分)

1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any

PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)

简单DP. 注意:If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence. #include<iostream> #include<cstring> #include<cmath> #include<al

PAT (Advanced Level) 1113. Integer Set Partition (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; long long a[100000+10]; long long sum=0,sum2; i

PAT (Advanced Level) 1040. Longest Symmetric String (25)

暴力. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; char s[2000]; int ans,len,sum; void check(int a,int b) { if(s[a]!=s[b]) return; if(a==b) sum=1; else sum=2; int left=a-1,right=b+1; while(!(left<0||right&