poj 3253 Fence Repair (優先隊列)
///優先隊列,每次取兩個最小的木板的長度,再把這個長度放進隊列,反復取
# include
# include
# include
# include
# include
# include
using namespace std;
///長度小的放在前面
class cmp
{
public:
bool operator ()(const __int64 a,const __int64 b)const
{
return a>b;
}
};
int main()
{
int n;
__int64 temp,sum,p1,p2;
while(~scanf("%d",&n))
{
priority_queue<__int64,vector<__int64>,cmp>q;///定義優先隊列
while(n--)
{
scanf("%I64d",&temp);
q.push(temp);
}
sum=0;
while(q.size()>1)
{
p1=q.top();
q.pop();
p2=q.top();
q.pop();
temp=p1+p2;
sum+=temp;
q.push(temp);
}
printf("%I64d\n",sum);
}
return 0;
}