• 过河


    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
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int x[1005];
    int main(){
        int t;cin>>t;int n;
        while(t--){
            cin>>n;
            for(int i=0;i<n;i++){
                cin>>x[i];
            }
            sort(x,x+n);
            int time=0;
            while(n){
                if(n==1){
                    time+=x[0];
                    break;
                }else
                if(n==2){
                    time+=x[1];
                    break;
                }else if(n==3){
                    time+=x[0]+x[1]+x[2];
                    break;
                }else {
                    time=time+min(x[n-1]+2*x[0]+x[n-2],2*x[1]+x[0]+x[n-1]);
                    n-=2;
                }
            }
                cout<<time<<endl;
        }
    return 0;
    }
    View Code
  • 相关阅读:
    FasDfs缩略图解决方案 -- Linux
    FastDFS 配置 Nginx 模块,并实现分布式同步-Linux
    Linux简单文本处理
    Linux命令执行顺序与管道命令
    建立Linux计划命令crontab
    Linux下的帮助命令
    Linux文件系统操作与磁盘管理
    Linux之文件的压缩与解压缩
    Linux环境变量与文件查找
    Linux目录结构及文件操作
  • 原文地址:https://www.cnblogs.com/demodemo/p/4655717.html
Copyright © 2020-2023  润新知