URAL 1826. Minefield(数学啊 递归)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1826

1826. Minefield

Time limit: 0.5 second

Memory limit: 64 MB

To fulfill an assignment, a reconnaissance group of n people must cross the enemy‘s minefield. Since the group has only one mine detector, the following course of action is taken: two agents
cross the field to the enemy‘s side and then one agent brings the mine detector back to the remaining group. This is repeated until only two agents remain. These two agents then cross the field together.

Each person gets across the field at their own speed. The speed of a pair is determined by the speed of its slower member.

Find the minimal time the whole group needs to get over the minefield.

Input

The first line contains the integer n (2 ≤ n ≤ 100). The i-th of the following n lines specifies the time the i-th member of the group needs to get over
the minefield (the time is an integer from 1 to 600).

Output

Output the minimal total time the group needs to cross the minefield.

Sample

input output
4
1
10
5
2
17

题意:

有n个人要经过一片雷区,但是他们只有一个扫雷用的探测器,所以每次只能过去两个人,其中一个人把探测器拿回来,继续再过去两个人,

每个人的行走速度不同,所花费的时间,是按照两个人中较慢的那个人所用时间计算的!

求全部人通过雷区的最短时间!

PS:

每次先把最大的两个移过去,当人数大于等于4的时候,

分两种情况:

1:最小的来回几次把最大的两个带过去。

2:先让最小的两个过去,然后最小的把探测器的回来,然后最大的两个过去,对面最小的把探测器带回来。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[147];
int ans = 0;
void cal(int m)
{
    if(m == 1)
    {
        ans+=a[0];
    }
    else if(m == 2)
    {
        ans+=a[1];
    }
    else if(m == 3)
    {
        ans+=a[0]+a[1]+a[2];
    }
    else
    {

        if((a[0]*2+a[m-1]+a[m-2]) < (a[0]+2*a[1]+a[m-1]))
        {
            ans+=a[0]*2+a[m-1]+a[m-2];//0号一个人来回
        }
        else
        {
            ans+=a[0]+2*a[1]+a[m-1];//0,1号两个人来回
        }
        cal(m-2);
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
        cal(n);
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-12-26 09:56:17

URAL 1826. Minefield(数学啊 递归)的相关文章

URAL 1826. Minefield 贪心

1826. Minefield Time limit: 0.5 second Memory limit: 64 MB To fulfill an assignment, a reconnaissance group of n people must cross the enemy's minefield. Since the group has only one mine detector, the following course of action is taken: two agents

URAL - 1826 Minefield

Minefield Time Limit: 500MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description To fulfill an assignment, a reconnaissance group of n people must cross the enemy's minefield. Since the group has only one mine detector,

9.20 函数 时间、数学、递归、字符串

函数:     四要素:返回类型      函数名      参数      函数体 强类型语言函数写法访问修饰符 返回类型 函数名 (参数列表){            函数体                        } 弱类型语言函数写法function 1.无参数的函数2.有参数的函数3.有返回值的函数JS常用函数日期时间函数数学函数字符串函数 递归:   递归的本质:函数自己调自己(从别处看来的笑话:要想理解递归,首先要理解递归) 日期时间函数(需要用变量调用):var b = n

《具体数学》——递归问题

以后的搬砖转化一下思路,以一本书为主体,进行类似读书笔记式的讨论和总结,首先便从这本高德纳的<具体数学>开始吧. 大致翻了一下这本书,能够明显感觉到大师所著的书和国内的一些出版物是没办法比的.看了一下这本书的序言,发现这本书有一个很大的特色就是“数学涂鸦”,好像是有一部分读者在阅读该书时的注释被收录到了这本书当中,这些涂鸦中不仅仅有俏皮的玩笑,还不乏精巧的解题方略,与作者在论述清晰优雅而又诙谐的语调融为一体,大概才看了一节,感觉<具体数学>的确是一本值得仔细研读的好书. 那么下面我

URAL1826. Minefield 题解

原题: http://acm.timus.ru/problem.aspx?space=1&num=1826 1826. MinefieldTime limit: 0.5 secondMemory limit: 64 MBTo fulfill an assignment, a reconnaissance group of n people must cross the enemy's minefield. Since the group has only one mine detector, t

noip2013 转圈游戏

P1965 转圈游戏 625通过 1.8K提交 题目提供者该用户不存在 标签数论(数学相关)递归/分治2013NOIp提高组 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 洛谷有缺陷 题目描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置,……,依此类推.游戏规则如下:每一轮第 0 号位置上的小伙伴顺时针走到第 m 号位置,第 1 号位置小伙伴走到第 m

20172320 2017-2018-2 《Java程序设计》第十周学习总结

20172320 2017-2018-2 <Java程序设计>第十周学习总结 教材学习内容总结 1.集合是一种对象,类似于保存其他对象的存储库 - 集合的同构意味着这种集合保存类型全部相同的对象:异构意味着可以保存各种类型的对象 2.抽象数据类型(ADT)是由数据和在该数据上所实施的具体操作构成的集合. - ADT有名称.值域和一组允许执行的操作 - ADT上可以执行的操作与底层的实现分离开了 3.一个动态数据结构用链来实现,动态数据结构的大小规模随需要增长和收缩 4.线性数据结构 - 队列:

什么是Scheme?原来还可以这样应用!

Scheme定义 Scheme 编程语言是一种Lisp方言,诞生于1975年,由 MIT 的 Gerald J. Sussman 和 Guy L. Steele Jr. 完成.它是现代两大Lisp方言之一:另一个方言是Common Lisp. Scheme遵循极简主义哲学,以一个小型语言核心作为标准,加上各种强力语言工具(语法糖)来扩展语言本身. MIT曾用Scheme作为计算机系入门课程的编程语言.计算机程序语言界著名的魔法书<计算机程序的构造和解释>(又称SICP)正是利用Scheme来解

LA3882 约瑟夫数学递归法

首先,约瑟夫环的数学优化方法为: 为了讨论方便,先把问题稍微改变一下,并不影响原意:问题描述:n个人(编号0~(n-1)),从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数.求胜利者的编号. 我们知道第一个人(编号一定是(m-1)%n) 出列之后,剩下的n-1个人组成了一个新的约瑟夫环(以编号为k=m%n的人开始):    k k+1 k+2 ... n-2, n-1, 0, 1, 2, ... k-2 并且从k开始报0.现在我们把他们的编号做一下转换: k --> 0 k+1 --