merge sorting

本文详细介绍了归并排序的基本原理,使用C++代码展示了如何通过递归和合并操作对一个大小为N的数组进行排序。

归并排序原理比较简单,但是占用一个n的数组。

#include<bits/stdc++.h>
using namespace std;

const int N = 1e5 + 10;
int a[N];
int b[N];

void mergesort(int* a, int start, int end, int* b)
{
    if(start == end)
    {
        return;
    }
    int mid = (start+end)/2;
    mergesort(a, start, mid, b);
    mergesort(a, mid+1, end, b);
    int i=start, j=mid+1, k = start;
    while(i <= mid && j <= end)
    {
        if(a[i]<a[j])
        {
            b[k++] = a[i];
            i++;
        }
        else
        {
            b[k++] = a[j];
            j++;
        }
    }
    while(i<=mid)
    {
        b[k++] = a[i];
        i++;
    }
    while(j<=end)
    {
        b[k++] = a[j];
        j++;
    }
    for(int m=start; m<=end; m++)
    {
        a[m] = b[m];
    }

}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  mergesort(a, 0, n-1, b);
  for (int i = 0; i < n; ++i) {
    cout << a[i] << ' ';
  }
}

The following is the current ranking rules for the ICPC Asia EC Online Qualifiers, and there will be two online contests.\n\nIn each contest, only the rank of the top-ranked team from each university will be taken as the score of that university;\nIn each contest, participating universities will be ranked according to their scores;\nThe two rankings of universities are combined using the merge sorting method. For any two universities that obtain the same ranking in different contests, the university that received this ranking in the first contest will be ranked first.\nDelete duplicate universities and obtain the final ranking of all participating universities (only the highest rankings for each university are retained).\nNow assuming that there are n teams in the first contest and m teams in the second contest.\nFor each contest, given the ranking of each team and the university to which it belongs, please output the final ranking of all participating universities according to the above rules.\nYou can better understand this process through the sample.\nInput\nThe first line contains two integers n,m (1≤n,m≤104) , representing the number of teams participating in the first contest and the second contest.\nThen following n lines, the i-th line contains a string si (1≤∣si∣≤10) only consisting of uppercase letters, representing the abbreviation of the university to which the i-th ranked team in the first contest belongs.\nThen following m lines, the i-th line contains a string ti (1≤∣ti∣≤10) only consisting of uppercase letters, representing the abbreviation of the university to which the i-th ranked team in the second contest belongs.\nIt’s guaranteed that each university has only one abbreviation.\nOutput\nOutput several lines, the i-th line contains a string, representing the abbreviation of the i-th ranked university in the final ranking.\nYou should ensure that the abbreviation of any participating universities appears exactly once. 翻译一下
08-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值