Poj 3404 bridge over a rough River (Bridge problem)

Source: Internet
Author: User

Question link: http://poj.org/problem? Id = 3404


Bridge over a rough River
Time limit:1000 ms   Memory limit:65536 K
Total submissions:4024   Accepted:1644

Description

A groupNTravelers (1 ≤N≤ 50) has approached an old and shabby bridge and wishes to cross the river as soon as possible. however, there can be no more than two persons on the bridge at a time. besides it's necessary to light the way with a torch for safe crossing but the group has only one torch.

Each traveler needsTiSeconds to cross the river on the bridge;I= 1 ,...,N(TiAre integers from 1 to 100). If two travelers are crossing together their crossing time is the time of the slowest traveler.

The task is to determine minimal crossing time for the whole group.

Input

The input consists of two lines: the first line contains the valueNAnd the second one contains the valuesTi(Separated by one or several spaces ).

Output

The output contains one line with the result.

Sample Input

46 7 6 5

Sample output

29

Source

Northeastern Europe 2001, western subregion


Ideas:

When N = 1, the answer is output directly.

When N = 2, the maximum output value is

When N = 3, output three and

When N> = 4, the two strategies are converted into four persons to find the optimal solution. If the speed of crossing a river is set to a <B <C <D, there are two strategies:

First AB, a, CD, and B, that is, temp = a + 2 * B + d

Ad first, a back, AC back, a back, that is, temp = 2 * A + C + D (forget this situation)

Then, take the minimum value. That is to say, select solution 1 when 2 * B <A + C; otherwise, select solution 2.


The Code is as follows:

#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <cstdlib>#include <climits>#include <ctype.h>#include <queue>#include <stack>#include <vector>#include <deque>#include <set>#include <map>#include <iostream>#include <algorithm>using namespace std;#define PI acos(-1.0)#define INF 0x3fffffff//typedef long long LL;//typedef __int64 LL;const int M = 1017;int main(){int n;int a[M];int i;while(~scanf("%d",&n)){for(i = 1; i <= n; i++){scanf("%d",&a[i]);}if(n == 1){printf("%d\n",a[1]);continue;}sort(a+1,a+n+1);if(n == 2){printf("%d\n",a[2]);continue;}if(n == 3){printf("%d\n",a[1]+a[2]+a[3]);continue;}int sum = 0;while(n){if(2*a[2] < a[1]+a[n-1]){sum+=a[1]+2*a[2]+a[n];n-=2;}else{sum+=2*a[1]+a[n-1]+a[n];n-=2;}if(n <= 3)break;}if(n == 1)sum+=a[1];else if(n == 2)sum+=a[2];else if(n == 3){printf("%d\n",sum+a[1]+a[2]+a[3]);continue;}printf("%d\n",sum);}return 0;}



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.