Crossing River POJ river issues

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

A very clever greedy issues, beginning to think that as long as the boat came back to make the shortest time like that make the smallest boat that has been followed by a walk, but not the case. . 
Solution: two types of process, the first is the above process, the second is that we let the fastest to cross the river with fast times, and then let the fastest back, let the slowest of slow cross the river with the times , let's come back fast times, both of which go smaller, provided that at least four people have a greater than or equal, if less than 4 people, then the simulation can
also pay attention to an odd number, if is odd, then the last will the remaining three individuals, namely the fastest times fast times and fast, if it is an even number, then because every time we reduce the two, and finally left in the fastest times faster
;
AC Code
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1E5+7;
int arr[N];
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++)    scanf("%d",&arr[i]);
        sort(arr+1,arr+1+n);
        int ans=0;
        if(n==1)    ans=arr[1];
        else if(n==2)    ans=arr[2];
        else if(n==3) {
            ans+=arr[3]+arr[1]+arr[2];
        }
        else {
            int sum1,sum2;
            for(int i=n;i>=4;i-=2){
                sum1=arr[i]+arr[i-1]+arr[1]+arr[1];
                sum2=arr[2]+arr[1]+arr[i]+arr[2];
                ans+=min(sum1,sum2);
            }
            if(n&1)
                ans+=arr[3]+arr[1]+arr[2];
            else ans+=arr[2];
        }
        cout<<ans<<endl;
    }
    return 0;
}

This blog talking about the good thief.

https://blog.csdn.net/Cworld2017/article/details/81503102

Guess you like

Origin www.cnblogs.com/Accepting/p/11565153.html