程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 1700 cross river (數學模擬)

POJ 1700 cross river (數學模擬)

編輯:C++入門知識

 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
#include

#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;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved