标签:
1 3 1 2 9
15
1 #include <stdio.h> 2 #include <algorithm> 3 using namespace std; 4 long long int num[12020]; 5 int main() 6 { 7 int t; 8 scanf("%d",&t); 9 while(t--) 10 { 11 int i, j, m; 12 scanf("%d", &m); 13 for(i=0; i<m; i++) 14 scanf("%lld", &num[i]); 15 sort(num, num+m); 16 long long int total = 0; 17 for(i=0; i<m; i++) 18 { 19 if(i == m-1) 20 break; 21 long long int temp = num[i]+num[i+1]; 22 num[i+1] = temp; 23 total += temp; 24 for(j=i+1; j < m; j++) 25 { 26 if(num[j] < num[j-1]) 27 { 28 long long int s; 29 s = num[j]; num[j] = num[j-1]; num[j-1]=s; 30 } 31 } 32 } 33 printf("%lld\n",total); 34 } 35 return 0; 36 }
//优先队列;
1 #include <queue> 2 #include <cstdio> 3 #include <iostream> 4 using namespace std; 5 int main() 6 { 7 int t; 8 scanf("%d", &t); 9 while(t--) 10 { 11 priority_queue<int, vector<int>, greater<int> >q; 12 int i, m; 13 scanf("%d", &m); 14 for(i=0; i<m; i++) 15 { 16 int num; 17 scanf("%d", &num); 18 q.push(num); 19 } 20 long long int total = 0; 21 while(q.size()!=1) 22 { 23 long long int temp = q.top(); 24 q.pop(); 25 temp += q.top(); 26 q.pop(); 27 q.push(temp); 28 total += temp; 29 } 30 printf("%lld\n",total); 31 } 32 return 0; 33 }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4684585.html