URAL 1826. Minefield (math, recursion)

Source: Internet
Author: User

Title Link: http://acm.timus.ru/problem.aspx?space=1&num=1826



1826. Minefieldtime limit:0.5 Second
Memory limit:64 MB
To fulfill an assignment, a reconnaissance group of NPeople must cross the enemy ' s minefield. Since the group have only one mine detector, the following course of action is taken:two agents cross the field to the ENE My ' s side and then one agent brings the mine detector back to the remaining group. This was repeated until only and agents remain. These and agents then cross the field together. Each person gets across the field at their own speed. The speed of a pair are determined by the speed of its slower member. Find the minimal time the whole group needs to get over the the minefield. Inputthe first line contains the integer N(2≤ N≤100). The I-th of the following Nlines specifies the time the I-TH member of the group needs to get through the minefield (the time is a integer from 1 to 600). Outputoutput The minimal total time the group needs to cross the minefield. Sample
input Output
411052
17


Test instructions

There are n people to go through a minefield, but they only have a mine-clearing detector, so each time only the past two people, one of them brought the detector back, continue to the past two people,

Each person's walking speed is different, and the time spent is calculated according to the time spent by the two people who are slower!

The shortest time to seek all people through the minefield!

Ps:

Each time the largest two is moved over, when the number is greater than or equal to 4,

In two cases:

1: The smallest back and forth several times to bring the largest two to the past.

2: First let the smallest two past, then the smallest put the detector back, then the largest two past, opposite the smallest put the detector back.


The code is as follows:

#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];// Number No. 0 A person back and forth        }        else        {            ans+=a[0]+2*a[1]+a[m-1];//0,1 two persons back and forth        }        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;}


URAL 1826. Minefield (math, recursion)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.