• POJ-1700 Crossing River


    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
    对于n<4的情况我们可以直接计算出来。
    对于大于4我们有每次都有两种策略。
    ①最快和次快过去,最快回;最慢和次慢过去,次快回,t=s[2]+s[1]+s[n]+s[2]。
    ②最快和最慢过去,最快回;次慢和最快过去,最快回,t=s[n]+s[0]+s[n-1]+s[0]。选择两者中用时较少的一个策略执行。
    每次决策后我们都发现过去了两个人。
    于是n-=2;
    再次循环。直到n<4;

    代码:
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<algorithm>
     5 using namespace std;
     6 int a[1200];
     7 int main(){
     8     int T;
     9     cin>>T;
    10     while(T--){
    11         int n;
    12         cin>>n;
    13         for(int i=1;i<=n;i++){
    14             scanf("%d",a+i);
    15         }
    16         sort(a+1,a+1+n);
    17         int sum=0;
    18         while(n){
    19             if(n==1){
    20                 sum+=a[1];
    21                 break;
    22             }
    23             else if(n==2){
    24                 sum+=a[2];
    25                 break;
    26             }
    27             else if(n==3){
    28                 sum+=a[1]+a[2]+a[3];
    29                 break;
    30             }
    31             else{
    32                 sum+=min(a[2]+a[1]+a[n]+a[2],a[n]+a[1]+a[n-1]+a[1]);
    33                 n-=2;
    34             }
    35         }
    36         cout<<sum<<endl;
    37     }
    38     return 0;
    39 }
  • 相关阅读:
    防火墙(Iptables NAT)
    zookeeper-分布式协调技术的搭建
    Cobbler 自动安装 配置
    GPG非对称加密
    服务管理--Nginx
    NTP时间服务器
    Javascript-数据类型、类型转换
    移动端计算页面尺寸
    javascript 给关键字加链接
    JS三元运算符
  • 原文地址:https://www.cnblogs.com/ISGuXing/p/7306077.html
Copyright © 2020-2023  润新知