POJ 1700 cross river (mathematical simulation)

Source: Internet
Author: User

ZookeeperCrossing River

Time Limit:1000 MS Memory Limit:10000 K
Total Submissions:10311 Accepted:3889

Description

A group of N people wishes to go into ss 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 started ss.

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

141 2 5 10

Sample Output

17


Train of Thought: (for example, 1-n people's time [n], in ascending order) when there is only one person: sum = time [1]; when there are two people: when sum = time [1] + time [2] 3: sum = time [1] + time [2] + time [3] Important! When there are more than four people, there are only two shipping methods in pursuit of the shortest time: (1) the fastest, the second is the fastest --> the fastest-> the slowest, slow trip --> fast time [2] + time [1] + time [n] + time [n-1] + time [2] (2) fastest, slowest --> fastest, fast --> fastest return time [n] + time [1] + time [n-1] + time [1]
#include
 
  #include#define maxn 100001using namespace std;int time[maxn];int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n,sum=0;        scanf("%d",&n);        for(int i=1;i<=n;i++)            scanf("%d",time+i);        sort(time+1,time+n+1);        while(n)        {            if(n==1)            {                sum+=time[1];                n=0;            }            else if(n==2)            {                sum+=time[2];                n=0;            }            else if(n==3)            {                sum+=time[1]+time[2]+time[3];                n=0;            }            else            {                if(time[2]*2>=time[1]+time[n-1])                    sum+=2*time[1]+time[n]+time[n-1];                else                    sum+=2*time[2]+time[1]+time[n];                n-=2;            }        }        printf("%d\n",sum);    }    return 0;}
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.