您的位置:首页 > 其它

nyoj 55 懒省事的小明 (优先队列||直接插入)

2015-04-25 10:05 211 查看
//优先队列
#include <stdio.h>//老大们可以注意了 ,一定要用long long 啊啊啊  我错了半页  就是一直用的int
#include <queue>
using namespace std;
priority_queue<int,vector<int>,greater<int> >s;//先出小的。。感觉这样解释比较容易懂,greater换成less就是先出大的。
int main()
{
	long long test,n,sum,x,y;
	scanf("%lld",&test);
	while(test--)
	{
		scanf("%lld",&n);
		for(int i=0;i<n;i++)
		scanf("%lld",&x),s.push(x);//
		if(n==1)
		{
			printf("%d\n",s.top());
			s.pop();
			continue;
		}
		sum=0;
		while(!s.empty())
		{
			x=s.top(),s.pop();
			y=s.top(),s.pop();
			sum+=x+y;
			if(!s.empty())
			s.push(x+y);
		}
		printf("%lld\n",sum);
	}
	return 0;
}
----------------------------------------------------------------------------------------
#include <stdio.h>//直接插入  以前做的 因为vc不支持long long 就用的double  你懂得。。。
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
	double a[12001],b[12001],sum;
	int t,i,q,j,n,temp;
	scanf("%d",&t);
	while(t--)
	{
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		scanf("%d",&n);
		for(i=0;i<n;i++)
			scanf("%lf",&a[i]);
		sort(a,a+n);//想插入 肯定要排序啦
		for(i=1,j=0;i<n;i++)
		{
			b[j++]=a[i]+a[i-1];
			a[i]=a[i]+a[i-1];
			for(q=i;q<n-1;q++)//慢慢寻找0.0
			{
				if(a[q]>a[q+1])
					temp=a[q],a[q]=a[q+1],a[q+1]=temp;
				else
					break;
			}
		}
		sum=0;
		for(i=0;i<j;i++)
			sum+=b[i];
		printf("%.lf\n",sum);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: