60pts ! TLE ! 求助 !
查看原帖
60pts ! TLE ! 求助 !
983282
hjm777NOIP 加油!楼主2023/10/1 19:46

最后三个点 TLE,求调

#include <iostream>
#include <algorithm>
#include <cstring>
#define lint long long
using namespace std;
lint n2, a1[10000010], a2[10000010], sum;
inline lint read(){
	lint now = 0, nev = 1; char c = getchar();
	while(c < '0' || c > '9') { if(c == '-') nev = -1; c=getchar(); }
	while(c >= '0' && c <= '9') { now = (now << 1) + (now << 3) + (c & 15); c = getchar(); }
	return now * nev;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	lint n = read();
	memset(a1, 127, sizeof(a1));
	memset(a2, 127, sizeof(a2));
	for(lint i = 0; i < n; i++) a1[i] = read();
	sort(a1, a1 + n);
	lint i = 0, j = 0, k, w;
	for(k = 1; k < n; k++) {
		w = a1[i] < a2[j] ? a1[i++] : a2[j++];
		w += a1[i] < a2[j] ? a1[i++] : a2[j++];
		a2[n2++] = w;
		sum += w;
	}
	cout << sum;
	return 0;
}
2023/10/1 19:46
加载中...