POJ 1700 Cross River (mathematical simulation)

Source: Internet
Author: User

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

Description

A group of N people wishes to go across a and only one boat, which can at most carry both persons. Therefore some sort of shuttle arrangement must be arranged on order to row the boat back and forth so, all people may Cross. Each person had a different rowing speed; The speed of a couple are determined by the speed of the slower one. Your job is to determine a strategy this minimizes the time for these people to get across.

Input

The first line of the input contains a single integer t (1 <= t <=), the number of test cases. Then T cases follow. The first line of all case contains N, and the second line contains n integers giving the time for each people to cross t He river. Each case was preceded by a blank line. There won ' t is more than, people and nobody takes more than, 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


thought: (If 1-n personal time time[n]. Increment arrangement)
  • When there is only one person: sum=time[1];
  • Two persons: Sum=time[1]+time[2]
  • Three people's time: sum=time[1]+time[2]+time[3]
  • The point ! When more than four people. For the pursuit of shortest time. There are only two modes of delivery: (1) fastest. Fastest time to---> slowest. Times slow---times fast TIME[2]+TIME[1]+TIME[N]+TIME[N-1]+TIME[2]
  • (2) Fastest, slowest--fastest and fastest. Times-Quick go back to time[n]+time[1]+time[n-1]+time[1]

#include <cstdio> #include <algorithm> #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;}


POJ 1700 Cross River (mathematical simulation)

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.