cqyz oj | diving competition | greedy

Description

  He held a diving competition in ohide lake of Macedon. One of the projects is jumping into the water from the mountains, and then diving to reach the end. This is a group project, a team of n individuals. You must use an oxygen bottle dive, but each team has only one oxygen bottle. Up to two people use an oxygen bottle, but this time they must be synchronized swimming, so the time to reach the end of a single run slower equal to the time needed to end. Fortunately, they are very friendly, so any two people are willing to swim together. Arrange a submerged strategy, so that the last player to reach the end as soon as possible.

Input

  Line 1: A single integer n, the number of teams. The following n lines, each line an integer that represents the i-th individual travel time to the end of the Ti (0 <= n, Ti <= 1000).

Output

  Time to reach the end of the first team.

Sample Input 1

3
1
3
4

Sample Input 2

6
1
2
5
6
8
9

Sample Output 1

8

Sample Output 2

27


This question implies that Italy: it was amazing to go diving bottle sent back

Before ordering
at least four individuals, when two kinds of greedy strategy to take best:
1, two of the fastest to the slowest people who were shipped in the past come back, time-consuming T [n] + T [1 ] + T [n -1] + T [. 1]
2, and the fastest times faster over the first person, wherein a back past the slowest of the two together, then another with the bottle back, Processed T [2] + T [2 ] + T [n] + T [1]

#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int a[1005],sum=0;
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    while(n>=4){
        sum+=min(a[1]*2+a[n]+a[n-1], a[2]*2+a[1]+a[n]);
        n-=2;
    }
       //少于4个人直接讨论
    if(n==3)sum+=a[1]+a[2]+a[3];
    else sum+=a[2];//n==2
    printf("%d",sum);
    return 0;
}

Guess you like

Origin www.cnblogs.com/de-compass/p/11294680.html