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
  • 相关阅读:
    HNOI2019 JOJO
    十二省联考2019 骗分过样例
    十二省联考2019 皮配
    十二省联考2019 字符串问题
    十二省联考2019 春节十二响
    十二省联考2019 异或粽子
    HNOI2019 白兔之舞 dance
    HNOI2019 多边形 polygon
    HNOI2019 鱼 fish
    P4770 [NOI2018]你的名字
  • 原文地址:https://www.cnblogs.com/demodemo/p/4655717.html
Copyright © 2011-2022 走看看