码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1700 cross river (数学模拟)

时间:2016-02-13 20:43:49      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:


                                                                                                                          Crossing River
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10311   Accepted: 3889

Description

A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle arrangement must be arranged in order to row the boat back and forth so that all people may cross. Each person has a different rowing speed; the speed of a couple is determined by the speed of the slower one. Your job is to determine a strategy that minimizes the time for these people to get across.

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. The first line of each case contains N, and the second line contains N integers giving the time for each people to cross the river. Each case is preceded by a blank line. There won‘t be more than 1000 people and nobody takes more than 100 seconds to cross.

Output

For each test case, print a line containing the total number of seconds required for all the N people to cross the river.

Sample Input

1
4
1 2 5 10

Sample Output

17


思路:(假如 1-n个人时间time[n]。递增排列)
  • 仅仅有一个人的时候:sum=time[1];
  • 二个人的时候:       sum=time[1]+time[2]
  • 三的人的时候:       sum=time[1]+time[2]+time[3]
  • 重点!当大于四个人的时候。为了追求时间最短化。仅仅有两种运送方式:(1) 最快。次快去-->最快回--->最慢。次慢去-->次快   time[2]+time[1]+time[n]+time[n-1]+time[2]
  •                                                                                                          (2) 最快,最慢去-->最快回-->最快。次快去-->最快回     time[n]+time[1]+time[n-1]+time[1]

#include<cstdio>
#include<algorithm>

#define maxn 100001
using namespace std;
int time[maxn];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,sum=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",time+i);

        sort(time+1,time+n+1);

        while(n)
        {
            if(n==1)
            {
                sum+=time[1];
                n=0;
            }
            else if(n==2)
            {
                sum+=time[2];
                n=0;
            }
            else if(n==3)
            {
                sum+=time[1]+time[2]+time[3];
                n=0;
            }
            else
            {
                if(time[2]*2>=time[1]+time[n-1])
                    sum+=2*time[1]+time[n]+time[n-1];
                else
                    sum+=2*time[2]+time[1]+time[n];
                n-=2;
            }
        }

        printf("%d\n",sum);
    }

    return 0;
}


POJ 1700 cross river (数学模拟)

标签:

原文地址:http://www.cnblogs.com/mengfanrong/p/5188262.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!