题解 P1334 【瑞瑞的木板】

声明:本题解已经与其他题解重合,

且存在压行情况。


首先,这个题解是我有了惨痛的教训:全部WA...
先发一个CODE做声明:

#include <bits/stdc++.h>
//万能头文件,优先队列OI请用<queue>/<algorithm>
#define lli long long int
using namespace std;
priority_queue<lli,vector<lli>,greater<lli> >cz;
void work()
{
	lli n,x,y,ans=0,temp;
	scanf("%lld",&n);
	for (lli i=1;i<=n;i++)
	{scanf("%lld",&temp);cz.push(temp);}
	for (lli i=1;i<=n-1;i++)
	{
		x=cz.top();cz.pop();
		y=cz.top();cz.pop();
		ans+=(x+y);
		cz.push(x+y);
	}
	printf("%lld",ans);
} 
int main()
{
	work();
	return 0;
}

问题出在哪里呢?看我原先的代码:

#include <bits/stdc++.h>
#define lli long long int
using namespace std;
priority_queue<lli,vector<lli>,greater<lli> >cz;
lli work(lli n)
{
	lli x,y,ans=0,temp;
	/*完全相同*/
	return ans;
} 
int main()
{
	lli n;scanf("%lld",n); 
	printf("%lld",work(n));
	return 0;
}

这样运行出来的数据会非常令人难受,
因为long long int 类型的数据很难转移,
我们传给work里面的数据有可能是一个错误的数字,
甚至说程序直接卡崩(RE)...
所以解决方案:不要在主函数里输入输出,直接在work运行即可...

原文地址:https://www.cnblogs.com/jelly123/p/10385887.html