zoukankan      html  css  js  c++  java
  • POJ 1700 cross river (数学模拟)

    
                                                                                                                              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
    ",sum);
        }
    
        return 0;
    }
    


  • 相关阅读:
    自己搭建二维码接口
    HTML CSS SPRITE 工具
    Codeforces Round #636 (Div. 3) 题解
    Codeforces Round #612 (Div. 1+Div. 2)
    计树问题小结 version 2.0
    Educational Codeforces Round 85 (Rated for Div. 2) 题解
    luogu6078 [CEOI2004]糖果
    luogu [JSOI2012]分零食
    多项式全家桶
    生成函数小结
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5188262.html
Copyright © 2011-2022 走看看