游客 Signup | Login
中文 | En

1861 - 过河问题

通过次数

0

提交次数

0

Time Limit : 3 秒 Memory Limit : 128 MB

在漆黑的夜里,N位旅行者来到了一座狭窄而且没有护栏的桥边。如果不借助手电筒的话,大家是无论如何也不敢过桥去的。不幸的是,N个人一共只带了一只手电筒,而桥窄得只够让两个人同时过。如果各自单独过桥的话,N人所需要的时间已知;而如果两人同时过桥,所需要的时间就是走得比较慢的那个人单独行动时所需的时间。问题是,如何设计一个方案,让这N人尽快过桥。

Input

第一行是一个整数T(1<=T<=20)表示测试数据的组数
每组测试数据的第一行是一个整数N(1<=N<=1000)表示共有N个人要过河
每组测试数据的第二行是N个整数Si,表示此人过河所需要花时间。(0<Si<=100)

Output

输出所有人都过河需要用的最少时间

Examples

Input Format

1
4
1 2 5 10

Output Format

17

Solution C

#include<stdio.h>
int a[1100];
int main()
{
	int t,n,i,k;
	scanf("%d",&t);
		while(t--)
		{  
			scanf("%d",&n);
			for(i=0;i<n;i++)
				scanf("%d",&a[i]);
    if(n == 1)         //一个旅行者  
        printf("%d\n",a[0]);     
   else if(n == 2)    //两个旅行者  
      printf("%d\n", a[0] > a[1] ? a[0]: a[1]);  
   else               //多个  
   {  
       int min=a[0];//排个序 
	   if(a[i]<min)
	   {
		   k=a[i];
		   a[i]=min;
		   min=k;
	   }
       int sum = 0; 
        while(1)  
		 			       {
            if(n == 2)      //剩两个人  
            {  
                sum += a[1];  
                break;  
            }  
            else if(n == 3) //剩三个人  
            {  
               sum += a[0] + a[1] + a[2];  
               break;  
            }  
            else  
            {  
              int t1 = a[0]+a[1] +a[1] +a[n-1];   //方案1    
                sum += t1;  
								                n-=2;
            }  	
        		} 
       printf("%d\n",sum);
   }  
		}
return 0;
}

Solution C++

#include<stdio.h>
#include<algorithm>
#define MIN(a,b) a<b?a:b
using namespace std;
int main()
{
    int T,N,s[1005],t,i;
    scanf("%d",&T);
    while(T--)
    {
        t=0;
        scanf("%d",&N);
        for(i=0;i<N;i++)
            scanf("%d",&s[i]);
        sort(s,s+N);
        if(N==1)t+=s[0];
        else if(N==2)t+=s[1];
        else if(N==3)t+=MIN(s[1]*2+s[N-1],s[0]+s[N-1]+s[N-2]);
        else 
        {
            while(N>3)
            {
                t+=MIN(s[0]+s[1]*2+s[N-1],s[0]*2+s[N-1]+s[N-2]);
                N-=2;
            }
            if(N==2)t+=s[1];
            if(N==3)t+=MIN(s[1]*2+s[N-1],s[0]+s[N-1]+s[N-2]);
        }
        
        printf("%d\n",t);
    }

}

Solution Java

import java.util.Arrays;
import java.util.Scanner;
public class Main
{
	static Scanner sc=new Scanner(System.in);
	public static void main(String[] args)
	{
		int T=sc.nextInt();
		while(T-->0)
		{
			int N=sc.nextInt();
			getMinTimes(N);
		}
	}
	public static void getMinTimes(int x)
	{
		int arr[]=new int[x];
		int temp=0;
		for(int i=0;i<x;i++)
		{
			arr[i]=sc.nextInt();
		}
		Arrays.sort(arr);
		for(int i=arr.length-1;i>=0;i-=2)
		{
			if(i==0){
				System.out.println(temp+arr[0]);
				break;
			}else if(i==1){
				System.out.println(temp+arr[1]);
				break;
			}else if(i==2){
				System.out.println(temp+arr[1]+arr[0]+arr[2]);
				break;
			}else
			{
				if(2*arr[1]<=arr[0]+arr[i-1])
				{
					temp+=arr[0]+2*arr[1]+arr[i];
				}else{
					temp+=2*arr[0]+arr[i-1]+arr[i];
				}
			}
		}
	}
}