HIHO #1128 : 二分·二分查找(快速排序一半)

来源:互联网 发布:参不敏 何足以知之 编辑:程序博客网 时间:2024/05/16 08:54

题目链接

快速排序应用

#include<bits/stdc++.h>using namespace std;#define cl(a,b) memset(a,b,sizeof(a))#define LL long long#define pb push_back#define gcd __gcd#define For(i,j,k) for(int i=(j);i<k;i++)#define lowbit(i) (i&(-i))#define _(x) printf("%d\n",x)const int maxn = 1e6+10;const int inf  = 1 << 28;LL a[maxn];LL k;int kth_sort(int l,int r){    //printf("l = %d, r = %d\n",l,r);    if(l>r){        if(a[l]==k)return l+1;        else return -1;    }    int low = l,high = r;    int key = a[l];//select the key    while(low<high){        while(low<high&&a[high]>key)high--;        a[low] = a[high];        while(low<high&&a[low]<=key)low++;        a[high]=a[low];    }    a[low] = key;    if(k<=key)kth_sort(l,low-1);    else kth_sort(low+1,r);}int main(){    int n;    while(~scanf("%d%lld",&n,&k)){        for(int i=0;i<n;i++){            scanf("%lld",&a[i]);        }        int ans = kth_sort(0,n-1);        printf("%d\n",ans);    }    return 0;}
0 0
原创粉丝点击