We have n piles of apples, and we want to get them together.
Once a time we can combine two piles of apples with a and b apples seperately together, using a+b forces. We want to know the minimum forces we use to combine all piles of apples together.
Input:
apple(n,lst)
where lst contains n numbers representing the number of apples in each pile
// TODO: Replace examples and use TDD development by writing your own tests Describe(any_group_name_you_want) { It(should_do_something) { int a[]={1,2,3,4}; Assert::That(apple(4,a), Equals(19)); int b[]={3,5,2,1,4}; Assert::That(apple(5,b), Equals(33)); } };
- // TODO: Replace examples and use TDD development by writing your own tests
- Describe(any_group_name_you_want)
- {
- It(should_do_something)
- {
- int a[]={1,2,3,4};
- Assert::That(apple(4,a), Equals(19));
- int b[]={3,5,2,1,4};
- Assert::That(apple(5,b), Equals(33));
- }
- };
We have n piles of apples, and we want to get them together.
Once a time we can combine two piles of apples with a and b apples seperately together, using a+b forces. We want to know the minimum forces we use to combine all piles of apples together.
Input:
apple(n,lst)
where lst contains n numbers representing the number of apples in each pile
#include<stdio.h>
#include<queue>
using namespace std;
int apple(int n,int* a)
{
priority_queue<int,vector<int>,greater<int> >q;
for(int i=0; i<n; i++)
q.push(a[i]);
int ans=0,tmp;
while(q.size()>=2)
{
tmp=0;
tmp+=q.top();
q.pop();
tmp+=q.top();
q.pop();
ans+=tmp;
q.push(tmp);
}
return ans;
}
// TODO: Replace examples and use TDD development by writing your own tests
Describe(any_group_name_you_want)
{
It(should_do_something)
{
int a[]={1,2,3,4};
Assert::That(apple(4,a), Equals(19));
}
};