Nyoj 47 River Crossing problem

Source: Internet
Author: User

Time limit: +Ms | Memory Limit:65535KB Difficulty:5
Describe

In the dark of the night, N-travelers came to a narrow and no-fence side of the bridge. If you don't use a flashlight, everyone is afraid to cross the bridge anyway. Unfortunately, a total of n individuals with only one flashlight, and the bridge is only narrow enough to allow two people at the same time. If they cross the bridge alone, the time required for n people is known, and if two people cross the bridge at the same time, it is the time required for the slower person to move alone. The question is, how to design a program that allows the N people to cross the bridge as quickly as possible.

Input
The
first line is an integer T (1<=t<=20) that represents the number of groups of test data
The first line of each set of test data is an integer n (1<=n<=1000) representing a total of n individuals to cross the river
The second row of each set of test data is n integer si, which indicates the time it takes the person to cross the river. (0<si<=100)
Output
the minimum time required for the output of all people crossing the river
Sample input
141 2 5 10
Sample output
17


A problem that feels more novel


Code:

#include <iostream> #include <algorithm>using namespace Std;int main () {int N,m,i,j,k;cin>>n;while ( n--) {cin>>m;int *a=new int[m];for (i=0;i<m;i++) cin>>a[i];sort (a,a+m); K=0;while (m>3) {if (2*a[1] <a[0]+a[m-2]) {k+=a[1];//a[0] and a[1] [River K+=a[0];//a[0] come back k+=a[m-1];//a[0] and a[n-1] River K+=a[1];//a[1] come back}else{k+=a[m-1] ;//a[0] and a[n-1] [river] k+=a[0];//a[0] come back k+=a[m-2];//a[0] and A[n-2] cross the river K+=a[0];//a[1] come back}m-=2;} if (3==m) K=k+a[0]+a[1]+a[2];else if (2==m) K=k+a[1];else if (1==m) k=k+a[0];cout<<k<<endl;delete[] A;} return 0;}



Nyoj 47 River Crossing problem

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.