为什么要先排序再放入优先队列啊?
查看原帖
为什么要先排序再放入优先队列啊?
759152
yinbe楼主2023/9/23 16:54
#include<iostream>
#include<queue>
using namespace std;
struct node
{
	long long x;
};
priority_queue<node,vector<node>,greater<node>>pq;
bool operator>(node a,node b)
{
	return a.x>b.x;
}
long long n,ans;
int t[100005];
node a;
int _read()
{ 
	int x=0;
	char s=getchar();
	while(s<'0'||s>'9')
	{
		s=getchar();
	}
	while(s>='0'&&s<='9')
	{
		x=x*10+s-'0';
		s=getchar();
	}
	return x;
}
int main()
{
//	ios::sync_with_stdio(false);
//	scanf("%lld",&n);
	n=_read();
	for(int i=1;i<=n;i++)
	{
//		scanf("%lld",&a[i].x);
		a.x=_read();
		pq.push({a.x});
//		t[a.x]++;
	}
//	for(int i=1;i<=100000;i++)
//	{
//		while(t[i])
//		{
//			t[i]--;
//			pq.push({i});
//		}
//	}
	while(pq.size()>1)
	{
		node min1=pq.top();
		pq.pop();
		node min2=pq.top();
		pq.pop();
		ans+=min1.x+min2.x;
		pq.push({min1.x+min2.x});
	}
	printf("%lld",ans);
	return 0;
}

我没排序和排序都TLE啊? 没区别啊?

2023/9/23 16:54
加载中...