P2527 [SHOI2001]Panda的烦恼

传送门

Solution

这东西就是丑数。
但是脑抽在看题解前忘了这东西,然后YY了一个\(KNlogK\)的做法,感觉会爆就没有写。
有一个\(O(alpha*K*N)\)\(alpha\)为常数,因为要判重)的做法:
对于每个\(a_i\),定义\(pos_i\)表示\(a_i\)乘到了第\(i\)大丑数。
每次扫一遍每个\(a_i\)乘上对应丑数的值(也就是生成了一个新丑数),找到最小值,判重后添加进\(ans\)丑数数组,同时对应的\(pos_i++\)
这样\(ans\)数组记录的丑数必定单调递增。
貌似用手写堆可以优化到\(KlogN\)
lazy就不写手写堆了

code

#include <iostream>
#include <cstdio>
using namespace std;

typedef long long LL;
const int MAXN = 100010;
const LL INF = 9999999999999999;
LL ans[MAXN];
int tot;
int a[MAXN];
int pos[MAXN];
int N, K;

int main() {
	#ifdef test
		freopen("test.txt", "r", stdin);
	#endif
	cin >> N >> K;
	for(int i = 1; i <= N; i++) scanf("%d", &a[i]);
	ans[0] = 1;
	while(tot < K) {
		LL Mn = INF, Mnid = 0;
		for(int i = 1; i <= N; i++)
			if(ans[pos[i]] * a[i] < Mn) 
				Mn = ans[pos[i]] * a[i], Mnid = i;
		pos[Mnid]++;
		if(ans[tot] == ans[pos[Mnid] - 1] * a[Mnid]) continue;
		ans[++tot] = ans[pos[Mnid] - 1] * a[Mnid];
		
	}
	cout << ans[K];
	return 0;
}
```cpp
posted @ 2021-01-11 22:19  Speculator18  阅读(81)  评论(0)    收藏  举报