#475. NOIP2008TG-27
NOIP2008TG-27
27.(完善程序)
(找第k大的数)给定一个长度为 1000000 的无序正整数序列,以及另一个数 n(1 ≤ n ≤ 1000000),接下来以类似快速排序的方法找到序列中第 n 大的数(关于第 n 大的数:例如序列 {1,2,3,4,5,6} 中第 3 大的数是 4)。
#include <stdlib.h>
#include <stdio.h>
int a[1000001],n,ans = -1;
void swap(int *a,int *b)
{
int c;
c = *a; *a = *b; *b = c;
}
int FindKth(int left, int right, int n)
{
int tmp,value,i,j;
if (left == right) return left;
tmp = rand()% (right - left) + left;
swap( &a[tmp], &a[left] );
value = ____________________
i = left;
j = right;
while (i < j)
{
while (i < j && ____________________ ) j --;
if (i < j) {a[i] = a[j]; i ++;} else break;
while (i < j && ____________________ ) i ++;
if (i < j) {a[j] = a[i]; j - -;} else break;
}
____________________
if (i < n) return FindKth( ____________________ );
if (i > n) return ____________________
return i;
}
int main()
{
int i;
int m = 1000000;
for (i = 1;i <= m;i ++)
scanf("%d", &a[i]);
scanf("%d", &n);
ans = FindKth(1,m,n);
printf("%d\n", a[ans]);
return 0;
}
①:{{ input(1) }}
②:{{ input(2) }}
③:{{ input(3) }}
④:{{ input(4) }}
⑤:{{ input(5) }}
⑥:{{ input(6) }}