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

题意:给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;
}


继续加油~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值