[C艹] 时间O(n),60' sub4全部TLE 求调
查看原帖
[C艹] 时间O(n),60' sub4全部TLE 求调
557826
D_guard楼主2022/7/28 20:17

rt,对时间复杂度计算不熟,如有误亦请指正

蒟蒻代码如下(评测记录见帖末):

#include <cstdio>
#include <iostream>
#include <vector>
#define ll long long

using namespace std;
const int MAXA = 1e5;
int n, a, pp, p = MAXA;
ll ans, num, hp,
	tmp[2], bin[MAXA + 1];
vector<ll> que;

int main()
{
	scanf("%d", &n);
	while (n--)
	{
		scanf("%d", &a);
		p = min(p, a);
		++bin[a];
	}
	while (1)
	{
		while (!bin[p])
			if (++p > MAXA)
				break;
		if (p > MAXA)
			break;
		tmp[pp] = p;
		--bin[p];
		if (pp)
		{
			num = tmp[0] + tmp[1];
			ans += num;
			if (num <= MAXA)
				++bin[num];
			else
				que.push_back(num);
		}
		pp = !pp;
	}
	while (hp < que.size())
	{
		tmp[pp] = que[hp++];
		if (pp)
		{
			num = tmp[0] + tmp[1];
			ans += num;
			que.push_back(num);
		}
		pp = !pp;
	}
	printf("%lld\n", ans);
	return 0;
}

评测记录见此

2022/7/28 20:17
加载中...