Merge fruit
This question obviously has no aftereffects for each choice.
There are several things to notice in the obvious greedy algorithm . After merging into a pile, a new pile will be formed. Two piles of merged
reordering here, according to the idea of this question itself, it is easy to think of insertion sort
insertion sort:
//这种插入排序是前面已知,将手中的牌插到前面
#include<iostream>
#include<algorithm>
using namespace std;
int a[105];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
//开始插入排序
for(int i=1;i<n;i++)
{
for(int j=i-1;j>=0&&a[j]>a[j+1];j--)
{
swap(a[j],a[j+1]);
}
}
for(int i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
return 0;
}
This question obviously needs to be inserted backwards
#include<iostream>
#include<algorithm>
using namespace std;
int a[105];
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
int res=0;
for(int i=1;i<n;i++)
{
a[i]+=a[i-1];
res+=a[i];
for(int j=i+1;j<n&&a[j]<a[j-1];j++)
{
swap(a[j],a[j-1]);
}
}
cout<<res<<endl;
return 0;
}