头像

SonderLau

东北往事




离线:2小时前


最近来访(0)

活动打卡代码 AcWing 788. 逆序对的数量

SonderLau
10小时前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
gg merge_sort(int q[], int l, int r)   // 归并排序
{
    if (l >= r) return 0;

    gg res = 0;

    int mid = (l + r) >> 1;

    int k = 0, i = l, j = mid + 1;

    res += merge_sort(q, l, mid) + merge_sort(q, mid + 1, r);

    while (i <= mid and j <= r) {
        if (q[i] <= q[j])
            tmp[k++] = q[i++];
        else{
            res += mid - i + 1;
            tmp[k++] = q[j++];
        }

    }

    while (i <= mid) tmp[k++] = q[i++];
    while (j <= r) tmp[k++] = q[j++];

    for (i = l, j = 0; i <= r; i++, j++) q[i] = tmp[j];

    return res;
}


活动打卡代码 AcWing 787. 归并排序

SonderLau
11小时前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
void merge_sort(int q[], int l, int r) 
{
    if(l >= r) return;

    int mid = (l + r) >> 1;

    int k = 0, i = l, j = mid + 1;

    merge_sort(q, l, mid);
    merge_sort(q, mid + 1, r);

    while (i <= mid and j <= r){
        if (q[i] <= q[j])
            tmp[k++] = q[i++];
        else
            tmp[k++] = q[j++];
    }

    while(i <= mid) tmp[k++] = q[i++];
    while(j <= r) tmp[k++] = q[j++];

    for(i = l, j = 0; i <= r; i++, j ++)
        q[i] = tmp[j];
}


活动打卡代码 AcWing 786. 第k个数

int quick_search(int l, int r, int k){
    if (l == r) return q[l];

    int x = q[l], i = l - 1, j = r + 1;

    while(i < j) {
        while(q[++i] < x);
        while(q[--j] > x);

        if(i < j ) swap(q[i], q[j]);
    }

    int lf = j - l + 1;
    if (k <= if) return quick_search(l, j, k);
    else return quick_search(j + 1, r, k - lf);
}


活动打卡代码 AcWing 785. 快速排序

//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

void quick_sort(int[] q, int l, int r){
    if (l >= r) return;

    int x = q[l], i = l - 1, j = r + 1;

    while(i < j) {
        do i++; while(q[i] < x);
        do j--l while(q[i] > x);
        if (i < j) swap(q[i], q[j]);
    }
    quick_sort(q, l, i);
    quick_sort(q, i+1, r);
}