[Merger] fruit NOIP 2004

[Merger] fruit NOIP 2004

topic

Title Description

In an orchard, a lot has shot down all the fruit, and different types of fruit into different piles. We decided to put a lot of fruit all synthetic pile.

Each combined, fruit lots can be combined together piles, consume physical strength equal to the weight of the fruit and piles. As can be seen, all the fruits were combined after n-1 times, on the left a pile. When a lot of fruit combined total consumption of physical strength is equal to the sum of each combined consumed.

But also because these fruits make great efforts to move back home, so a lot to save energy during the merge fruit as possible. Number and the number of each type of fruit Fruit wt assumed that each are 11, and it is known fruit, your task is to design a combined program order, that minimizes cost a lot of physical strength, physical and outputting the minimum cost value.

For example, three kinds of fruit, followed by the number 1, 2, 9. Can first 1, 2 stack, are combined, the number of the new stack 3, 3 taxing. Subsequently, the new stack and the third stack combined original, and to give the new stack number is 12, 12 is taxing. Therefore, a lot of total taxing = 3 + 12 = 15. 15 can be shown that the minimum value of the physical cost.

Input Format

A total of two lines.
The first row is an integer n (1≤n≤10000), represents the number of types of fruit.

The second line contains n integers, separated by spaces, the integer i ai (1≤ai ≤20000) is the number of i-th fruit.

Output Format

An integer, i.e. the value of the minimum physical cost. Ensure that the input data is smaller than 2 ^ {31}

Sample input and output

Copy Input # 1
. 3
1 2. 9
Output # 1 replication
15

Description / Tips

For 30% of the data, ensure n≤1000:

For 50% of the data, ensure n≤5000;

For all of the data, ensure n≤10000.

analysis

c ++ directly to do with the queue stl. priority_queue this queue number is put into each well will sort themselves.
Then before each take two out, merged into one number and put in the queue, while ans plus the value of this number,

//priority_queue<int>q;
//priority_queue<int,vector<int>,greater<int> >q; 小到大 排序
//priority_queue<int,vector<int>,less<int> >q;		大到小 排序

Code

#include<iostream>

using namespace std;
#include<algorithm>
#include<queue>

//priority_queue<int>q;
//priority_queue<int,vector<int>,greater<int> >q; 小到大 
//priority_queue<int,vector<int>,less<int> >q;		大到小


int n;
priority_queue<int,vector<int>,greater<int> >q;
int x,ans =0; 

int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>x;
		q.push(x);
	}
	while(q.size()>1){
		int a=q.top();
		q.pop();
		int b=q.top();
		q.pop();
		
		ans += a+b;
		q.push(a+b);
	}
	cout<<ans<<endl;
	return 0;
} 
Published 75 original articles · won praise 1 · views 3663

Guess you like

Origin blog.csdn.net/A793488316/article/details/104547827