URAL 1826 贪心经典题型

题意:有一个地雷区,n个人要过去,但是只有一个探雷检测器,那么同时最多只能有两个人过去,过去的人必须把探雷器拿回来,每个人过去的速度都不同,总体的速度决定于较慢的那个。

题解:每次,1.要么是 先去两个最小的a[1],a[2],a[1]又拿检测器回来,a[n],a[n-1]过去,最后a[2]回来又拿检测器回来,此时花费时间为 cost1 = a[1] + 2*a[2] + a[n];

                      2.要么是先去a[1],a[n],a[1]又回来,a[1]和a[n-1]过去,a[1]再回来,花费时间为 cost2 = a[1]*2 + a[n] +a[n-1];

为什么出现2 情况呢? 比如说 n=6 a={1,1000,1000,1000,1000,1000},由上可知,cost1 = 3001,cost2 = 2002,很明显 cost2更小!这题用递归解决更优!

 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <cstring>
#include <map>
#include <set>

#define LL long long
#define pb push_back
#define pf push_front
#define loop(a,b,c) for(int a=b;a<=c;a++)
#define rloop(a,b,c) for(int a=b;a>=c;a--)
#define clr(a,b) memset(a,b,sizeof a)
#define sd(x) scanf("%d",&x)
#define sf(x) scanf("%lf",&x)
#define ss(x) scanf("%s",&x)
#define inf 1<<30
#define fi first
#define se second
#define maxn 100005
using namespace std;
int a[1005];
int ans(int n)
{
		if(n==2) return a[2];
		if(n==3) return a[1]+a[2]+a[3];
		return ans(n-2) + min(a[1] + a[2]*2 + a[n],2*a[1]+a[n]+a[n-1]) ;
}
void solve()
{
		int n;
		cin>>n;
		loop(i,1,n) cin>>a[i];
		sort(a+1,a+n+1);
		printf("%d\n",ans(n));
}
int main()
{
    //freopen("data.txt","r",stdin);
    solve();
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值