HDU-6040 Hints of sd0061

来源:互联网 发布:苹果数据接口转usb 编辑:程序博客网 时间:2024/05/21 05:56

题意:给N个数字,M次查询,第i次查询问A数组中第B[i]小的数是多少。

思路:考察了快排的思想,STL的使用。

nth_element(A, A+K, A+N)

表示在数组A的[0, N-1]中找到第K小的(K从0开始算)并放在第K个位置,并且前K-1(共K个)个位置均为小于A[K]的数。复杂度将近线性。

nth_element(A+Q, A+K, A+N)

则表示从[A+Q, A+N-1]中找第K-Q(K-Q从0开始算)小的并放在第K个位置。

注意到在找到第K小的时候,前K-1个数均是小于A[K]的,这样我们从大往小枚举寻找,可以减少搜索量。


代码:

#include <bits/stdc++.h>using namespace std;const int maxn = 1e7+5;struct node{unsigned val, id;bool operator<(const node k)const{return val > k.val;}} B[105];unsigned A[maxn], ans[105];unsigned N, M, _A, _B, _C, K, UP;unsigned x, y, z;unsigned rng61() {unsigned t;x ^= x << 16;x ^= x >> 5;x ^= x << 1;t = x;x = y;y = z;z = t ^ x ^ y;return z;}int main(){int count = 0;while(~scanf("%u %u %u %u %u", &N, &M, &_A, &_B, &_C)){x = _A, y = _B, z = _C;for(int i = 1; i <= M; ++i){ scanf("%u", &B[i].val); B[i].id = i;}for(int i = 1; i <= N; ++i) A[i] = rng61();sort(B+1, B+M+1); UP = N+1;//nth_element(A, A + k, A+n)for(int i = 1; i <= M; ++i){K = B[i].val+1;nth_element(A+1, A+K, A+UP);UP = K; ans[B[i].id] = A[K];}printf("Case #%d:", ++count);for(int i = 1; i <= M; ++i) printf(" %u", ans[i]);puts("");}return 0;}


继续加油~

原创粉丝点击