第k顺序统计量是一个经典的算法,其本质上是利用类似于快速排序的方法实现的,但是我们可以使用STL模版
nth_element()轻松过题。
头文件<algorithm>
nth_element(a,a+k-1,a+3000000);
本质上使的整个数组的趋势上还是从小到大的,本题求解的是第k小的元素,本质上还是把a+k-1这个指针指向的元素安排上
当然第一小的元素就是nth_element() 数组首地址上所存放
#include <stdio.h> #include <algorithm> #include <string.h> using namespace std; int a[3000010]; int main(){ int A, B, C, k; while(~scanf("%d %d %d %d %d",&A,&B,&C,&a[1],&k)){ for(int i = 2; i <= 3000000; ++i) a[i] = ((1LL * a[i - 1] * A ^ B) + C) % 1000000007; nth_element(a,a+k-1,a+3000000); printf("%d ", a[k]); } return 0; }
的元素。