题目 https://cn.vjudge.net/problem/Gym-101492E
题目
n个人要过河 只有一条船 一次最多运两个人 时间按大的算 要有人把船开回来才能接着运
思路
考虑两种情况
1 花费时间最小的把船开回来 在带别人过去
2 花费时间最小的把船开回来 两个花费时间大的过去 花费时间第二小的开回来
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[100005];
int main()
{
ll sum,n,m,i,j,ans,t;
scanf("%lld",&n);
ans = 0;
for(i = 1; i <= n; i++)
{
scanf("%lld",&a[i]);
}
sort(a+1,a+1+n);
if(n <= 2)
{
printf("%lld\n",a[n]);
return 0;
}
ll x = a[1],y = a[2];
if(n % 2 == 1)
{
ans = a[1] + a[2] + a[3];
i = 4;
}
else
{
ans = a[2];
i = 3;
}
// cout<<ans<<endl;
for(; i<=n; i+=2)
{
if(x + a[i] + x + a[i+1] < x + 2*y + a[i+1])
{
ans += 2*x + a[i] + a[i+1];
// cout<<ans<<"!@"<<endl;
}
else
{
ans += x + 2*y + a[i+1];
// cout<<ans<<"#$"<<endl;
}
}
printf("%lld\n",ans);
return 0;
}