[UOJ #52]【UR #4】元旦激光炮

交互题求第K大元素
本文介绍了一种解决交互题的方法,通过比较三个有序数组的元素,每次剔除最小值,最终找到第K大的元素。算法使用C++实现,详细展示了如何在限定次数内定位目标元素。

题目大意:交互题,给你三个有序数组,长度分别为$n\_a,n\_b,n\_c$,都不超过$10^5$。三个函数$get\_a(i),get\_b(i),get\_c(i)$,分别返回$a_i,b_i,c_i$。

现在要你编写一个函数$query\_kth()$,求出三个数组中第$k$大的元素。

题解:每次求$\Big\lfloor\dfrac k 3\Big\rfloor$的$a,b,c$,把最小的舍去

卡点:下标移动时写错

 

C++ Code:

#include "kth.h"
#include <cstdio>
#include <algorithm>
int query_kth(int n_a, int n_b, int n_c, int k) {
	int a = 0, b = 0, c = 0, num[10], tot = 0;
	while (k >= 3) {
		int t = k / 3;
		int A = get_a(a + t - 1), B = get_b(b + t - 1), C = get_c(c + t - 1);
		if (A < B) {
			if (A < C) a += t;
			else c += t;
		} else {
			if (B < C) b += t;
			else c += t;
		}
		k -= t;
	}
	for (int i = 1; i <= k; i++) {
		if (a < n_a) num[++tot] = get_a(a++);
		if (b < n_b) num[++tot] = get_b(b++);
		if (c < n_c) num[++tot] = get_c(c++);
	} 
	std::sort(num + 1, num + tot + 1);
	return num[k];
	return 0;
}

  

转载于:https://www.cnblogs.com/Memory-of-winter/p/9804505.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值