您的位置:首页 > 大数据 > 人工智能

HDU-6040 Hints of sd0061 - 2017 Multi-University Training Contest - Team 1(快排思想STL应用)

2017-07-28 15:49 519 查看
题意:给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].va
4000
l);
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;
}


继续加油~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐