蓝桥杯ALGO-1,区间k大数查询

#include<stdio.h>

int devide(long a[], int low, int high) {
    long key = a[high];
    while (low<high) {
        while (low<high&&a[low]>key) {
            low++;
        }
        if (low<high)
            a[high--] = a[low];
        while (low<high&&a[high]<key) {
            high--;
        }
        if (low<high)
            a[low++] = a[high];
    }
    a[high] = key;
    return high;
}

void sort(long a[], int low, int high) {
    if (low>high) {
        return;
    }
    int j;
    j = devide(a, low, high);
    sort(a, low, j - 1);
    sort(a, j + 1, high);
}

int main() {
    int n, i, j;
    long a[1000], b[1000];
    int m;
    int l, r, k;
    scanf("%d", &n);
    for (i = 0; i<n; i++) {
        scanf("%ld", &a[i]);
    }
    scanf("%d", &m);
    for (i = 0; i<m; i++) {
        for (j = 0; j<n; j++) {
            b[j] = a[j];
        }
        scanf("%d%d%d", &l, &r, &k);
        sort(b, l - 1, r - 1);
        printf("%d\n", b[l + k - 2]);
    }
    return 0;

}

 

posted @ 2017-02-16 11:18  Carol淋  阅读(227)  评论(0编辑  收藏  举报