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
  • 相关阅读:
    微信小程序之base64转为本地图片
    微信小程序之页面跳转方法
    一些收集的社区网址+学习网站、文档
    11-DOM介绍
    10-关于DOM的事件操作
    09-伪数组 arguments
    前端---css
    前端---html
    并发编程------网络IO模型、IO多路复用
    并发编程------协程
  • 原文地址:https://www.cnblogs.com/demodemo/p/4655717.html
Copyright © 2011-2022 走看看