2534 渡河 2013年市队选拔赛广州

2534 渡河

2013年市队选拔赛广州

时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold
 
 
 
 
题目描述 Description

    有N个人需要渡河到对岸,但是只有一条小船,只能乘坐两人,请问怎么能够让他们以最少的时间渡河到对岸呢?

输入描述 Input Description

输入为两行,第一行为渡河的人数N (1<=N<=1000)

第二行为N个正整数,范围是 [1,100],代表他们划船到对岸需要多少分钟

输出描述 Output Description

输出只有一行,代表最短的渡河时间,单位为分钟

样例输入 Sample Input

3

2 3 50

样例输出 Sample Output

55

数据范围及提示 Data Size & Hint

N (1<=N<=1000)

 1 #include<cstdio>
 2 #include<iostream> 
 3 #include<cmath>
 4 #include<algorithm>
 5 
 6 using namespace std;
 7 
 8 int speed[1001];
 9 
10 int main()
11 {
12     int t, n, tmp;
13         scanf("%d", &n);
14         for(int i=0; i<n; i++)
15         {
16             scanf("%d", &speed[i]);
17         }
18         for(int i=0; i<n; i++)
19         {
20             for(int j=i; j<n; j++)
21             {
22                 if(speed[i] > speed[j])
23                 {
24                     tmp = speed[i];
25                     speed[i] = speed[j];
26                     speed[j] = tmp;
27                 }
28             }
29         }
30         // greedy
31         int start = n, ans = 0;
32         while(start)
33         {
34             // start = 1,2,3时直接处理 
35             if(start == 1)
36             {
37                 ans  += speed[0];
38                 break; 
39             }
40             else if(start == 2)
41             {
42                 ans += speed[1];
43                 break;
44             }
45             else if(start == 3)
46             { // 0,2过河,0回程,0,1过河 
47                 ans += speed[2]+speed[0]+speed[1];
48                 break;
49             }
50             // start>3根据策略选择 
51             else{
52                 ans += min(speed[1]+speed[0]+speed[start-1]+speed[1], speed[start-1]+2*speed[0]+speed[start-2]);
53                 start -= 2; 
54             } 
55         } 
56         printf("%d
", ans);
57     
58     return 0;
59 }
原文地址:https://www.cnblogs.com/lyqlyq/p/6817020.html