每个点都差大约50ms,如何改进卡常
查看原帖
每个点都差大约50ms,如何改进卡常
743373
Vitamin_B楼主2024/5/22 14:21

rt

# pragma GCC optimize("Ofast,no-stack-protector")

# pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

# include <bits/stdc++.h>

using namespace std;

typedef long long ll;

typedef pair <int, int> pii;

int n, t[100005];

ll x, y, s, sum, maxn;

queue <ll> a, q;

int main () {

	ios::sync_with_stdio (0);

	cin.tie (0);

	cout.tie (0);

	cin >> n;

	for (int i = 0; i < n; ++ i)
		cin >> x, ++ t[x], maxn = max (maxn, x);

	for (int i = 1; i <= maxn; ++ i)
		while (t[i] --)
			a.push (i);

	while (-- n) {

		if (a.empty ())
			x = q.front (), q.pop ();
		else if (q.empty ())
			x = a.front (), a.pop ();
		else if (a.front () < q.front ())
			x = a.front (), a.pop ();
		else
			x = q.front (), q.pop ();

		if (a.empty ())
			y = q.front (), q.pop ();
		else if (q.empty ())
			y = a.front (), a.pop ();
		else if (a.front () < q.front ())
			y = a.front (), a.pop ();
		else
			y = q.front (), q.pop ();

		s = x + y;

		q.push (s);

		sum += s;

	}

	cout << sum;

	return 0;

}
2024/5/22 14:21
加载中...