HDU6040 Hints of sd0061【STL】

3 篇文章 0 订阅

题意:用给的A、B、C算出n个值,每次求第Bi小的数


思路:直接sort的话,会超时。这里用到STL中的函数

nth_element ( arr.begin() ,arr+n,arr.end() ),这个时间复杂度是线性的
调用函数后保证比第n小的数小的都在前面,比它大的数在后面。

想想也是没必要全部排好,就求那一个就好。给B排序后(用间接排序),从后面往前求。


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<stdlib.h>
#include<math.h>
#include<vector>
#include<list>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<numeric>
#include<functional>
using namespace std;
typedef long long ll;
const int maxn = 1e7+5;
unsigned s[maxn],ans[maxn];
unsigned x, y, z;
int pai[maxn],a[maxn];

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 cmp(const int &x,const int &y)
{
	return a[x] < a[y];
}

int main(void)
{
	int n,m,i,j,k,kase = 1;
	while(scanf("%d%d%u%u%u",&n,&m,&x,&y,&z)!=EOF)
	{
		for(i = 0; i < m; i++)
		{
			pai[i] = i;
			scanf("%d",&a[i]);
		}
		for(i = 0; i < n; i++)
			s[i] = rng61();
		sort(pai,pai+m,cmp);
		int last = n;
		for(i = m-1; i >= 0; i--)
		{
			int now = a[pai[i]];
			if(i < m-1 && now == a[pai[i+1]])
				ans[pai[i]] = ans[pai[i+1]];
			else
			{
				nth_element(s,s+now,s+last);
				last = now;
				ans[pai[i]] = s[now];
			}
		}
		printf("Case #%d:",kase++);
		for(i = 0; i < m; i++)
			printf(" %u",ans[i]);
		putchar('\n');
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值