牛客练习赛94 「Nhk R1 D」Apocryphal Vir Pulcher
题意:给出一个序列a,它的元素的所有线性组合构成集合x,求x的第k大数
题解:
构造n+1个队列,第i个队列代表前i个数的线性组合
初始条件下,仅有q[0] = 0
每次操作:找出最小的队头(设其为r)并出队,设它在q[d],则对d<i<=n,将a[i]+r加入q[i]
第k次取出的r即为答案
官方代码
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <ctime>
#include <queue>
#define LL long long
using namespace std;
const int MAXN = 85;
queue <LL> que[MAXN];
int n, k, a[MAXN], num;
LL minn;
int main() {
scanf("%d%d", &n, &k); k --;
for(int i = 1; i <= n; i ++) scanf("%d", &a[i]);
que[0].push(0);
for(int i = 1; i <= k + 1; i ++) {
minn = 9e18;
for(int j = 0; j <= n; j ++) if(!que[j].empty() && que[j].front() < minn) minn = que[j].front(), num = j;
while(que[0].size()) que[0].pop();
que[num].pop();
for(int j = num; j <= n; j ++) que[j].push(minn + a[j]);
}
printf("%lld", minn);
return 0;
}