zoukankan      html  css  js  c++  java
  • 过河

    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
  • 相关阅读:
    PHP保留两位小数的几种方法
    解决PHP下载大文件失败,并限制下载速度
    MySQL索引
    商城商品部分-数据表创建
    string 函数
    array函数
    BTREE这种Mysql默认的索引方式,具有普遍的适用性
    mysql索引类型 normal, unique, full text
    typedef的意义和函数指针
    35. 搜索插入位置-7月17日
  • 原文地址:https://www.cnblogs.com/demodemo/p/4655717.html
Copyright © 2011-2022 走看看