poj 1700 Crossing River 过河问题。贪心

Crossing River
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9887   Accepted: 3737

Description

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

Source

 
每一趟的决策判断。
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 int f[1003];
 9 bool cmp(int n1,int n2)
10 {
11     return n1>n2;
12 }
13 int main()
14 {
15     int T;
16     int i,k,n,Sum,Min;
17     scanf("%d",&T);
18     while(T--)
19     {
20         scanf("%d",&n);
21         for(i=1;i<=n;i++)
22             scanf("%d",&f[i]);
23         sort(f+1,f+1+n,cmp);
24         if(n==1 || n==2)
25         {
26             printf("%d
",f[1]);
27             continue;
28         }
29         if(n==3)
30         {
31             printf("%d
",f[1]+f[3]+f[2]);
32             continue;
33         }
34         int min1=f[n];
35         int min2=f[n-1];
36         if(n&1) k=n-3;
37         else k=n-2;
38         for(i=1,Sum=0;i<=k;i=i+2)
39         {
40             Min=min(min2+min1+f[i]+min2,f[i]+min1+f[i+1]+min1);
41             Sum=Sum+Min;
42         }
43         if(n&1) Sum=Sum+f[n-2]+min1+min2;
44         else Sum=Sum+min2;
45 
46         printf("%d
",Sum);
47     }
48     return 0;
49 }
原文地址:https://www.cnblogs.com/tom987690183/p/3440518.html