【题目链接】:click here~~
【题目大意】:
1 3 1 2 9
15
【解题思路】:去年下学期写的,看了看之前写的代码,简直不忍直视,之前正好学 了多重集合multiset,于是就顺便把三种方法总结一下
make_heap是第一次使用,相关理解(转换从指定范围的元素为第一个元素是最大,而的堆的排序标准可能指定具有二进制谓词)
方法一:
/*
STL_make_heap
*/
#include <bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){ return a>b;}
int A[12005];
int shuru(){
int sum=0;
char ch;
while((ch=getchar())<'0'||ch>'9'); sum=ch-'0';
while((ch=getchar())>='0'&&ch<='9') sum=sum*10+ch-'0';
return sum;
}
int main()
{
//freopen("1.txt","r",stdin);
int n,m;
n=shuru();
while(n--){
m=shuru();
for(int i = 0; i < m; i ++)
A[i]=shuru();
make_heap(A,A+m,cmp);//构造堆函数
long long sum =0;
while(m>1)
{
pop_heap(A,A+m,cmp);
m--;
pop_heap(A,A+m,cmp);
A[m-1]+=A[m];
sum+=A[m-1];
push_heap(A,A+m,cmp);
}
printf("%lld\n",sum);
}
return 0;
}
方法二:
/*
STL_priority_queue
*/
#include <bits/stdc++.h>
using namespace std;
priority_queue<int ,vector<int >,greater<int > > ver;
int shuru(){
int sum=0;
char ch;
while((ch=getchar())<'0'||ch>'9'); sum=ch-'0';
while((ch=getchar())>='0'&&ch<='9') sum=sum*10+ch-'0';
return sum;
}
int main()
{
//freopen("1.txt","r",stdin);
int n,m,d,t;
n=shuru();
while(n--)
{
while(!ver.empty()) ver.pop();
m=shuru();
for(int i=0;i<m;i++) {
d=shuru();
ver.push(d);
}
long long s=0;
while(ver.size()>1){
t=ver.top();
ver.pop();
t+=ver.top();
ver.pop();
s+=t;
ver.push(t);
}
printf("%lld\n",s);
}
return 0;
}方法三:
/*
STL_multiset
*/
#include <bits/stdc++.h>
using namespace std;
#define LL long long
int shuru(){
int sum=0;
char ch;
while((ch=getchar())<'0'||ch>'9'); sum=ch-'0';
while((ch=getchar())>='0'&&ch<='9') sum=sum*10+ch-'0';
return sum;
}
int main()
{
//freopen("1.txt","r",stdin);
int t, n;
LL temp, sum;
multiset<LL>VER;
multiset<LL>::iterator it;
t=shuru();
while(t--)
{
n=shuru();
while(n--)
{
scanf("%lld",&temp);
VER.insert(temp);
}
sum = 0;
while(VER.size() > 1)
{
it = VER.begin();
temp = *it;
VER.erase(it);
it = VER.begin();
temp += *it;
VER.erase(it);
sum += temp;
VER.insert(temp);
}
VER.clear();
printf("%lld\n", sum);
}
return 0;
}
从一道整数合并问题学习 STL 之make_heap &&priority_queue&&multiset
原文:http://blog.csdn.net/u013050857/article/details/45113809