Gym 101492E Teamwork 伪dp解法

E. Teamwork
time limit per test
2.0 s
memory limit per test
512 MB
input
standard input
output
standard output

Teamwork is highly valued in the ACM ICPC. Since the beginning, the ICPC has distinguished itself from other programming contests in that teamwork is a key factor.

The University of Beijing, host of next year's World Finals, is planning recreational activities for the participants. They want to prepare games that show the importance of teamwork and enable new acquaintances among the contestants from all over the world. One of the staff members has been thinking about the following game.

In a court we have several N-person teams. The goal of each team is roughly as follows: starting from a corner, each team must take all of its members to the other corner of the court. The winning team is the one that finishes this in the shortest time.

Now we explain the game more formally. Consider the team that starts at corner A and must take all of its members to corner B. The following procedure is repeated:

  1. While there are team members at corner A,
    • If there is only one team member, he is the only one that goes to corner B.
    • Otherwise, two team members must tie one leg of each with a rope and go to B.
  2. Once arriving at B, they untie their legs, if applicable.
  3. If there are still team members at corner A, some team member at B must return to A with the rope.

The organization wants to form the teams so that no team has an advantage. They entrusted you with the following task. Given the time in seconds that the members of a team take to go from a corner to the other, find the minimum time it takes for the team to take all its members from corner A to corner B. When two team members cross the court with their legs tied, the time for the pair to cross is the maximum between the times of the two members.

Input

The first line of input contains an integer N, the number of team members. The next line contains N integers ti, the time in seconds that the i-th team member takes to go from one corner to the other.

  • 1 ≤ N ≤ 105
  • 1 ≤ ti ≤ 109
Output

Print a single integer, the minimum time required for the whole team to reach corner B.

Examples
input
Copy
3
30 40 50
output
Copy
120
input
Copy
4
10 20 50 100
output
Copy
170
Note

In the first example, the process can be completed in 120 seconds as follows:

  1. The first and second members go from A to B in 40 seconds.
  2. The first member returns to corner A in 30 seconds.
  3. The first and third team members go from A to B in 50 seconds.

This is not the only way to complete the process in this total time, but no other way does it in strictly less than 120 seconds.

题意:有A,B两个点,A有n个人要到B去,但是除非n==1,那么从A到B只能2个人一起,从B到A只能1个人。输入第一行表示A点有n个人,接下来n个数字是每个人从A到B(或从B到A)的时间,两个人从A到B的时间取其中较大的。

样例1:编号为1,2,3,首先1和3先过去,1回来,然后1和2过去,时间是50+30+40=120

样例2:编号为1,2,3,4,首先1和2先过去,1回来,然后3和4过去,2回来,最后1和2过去,时间是20+10+100+20+20=170

题目分析:这题看起来似乎是dp的题目,但是看内存和时间,2个循环的时间并不足够。但是稍微分析,其实可以发现,有两种情况:

假设有编号为1,2,3,4,4个人在A要去B;

第一种情况:先1+2过去,然后换3+4过去;具体流程是1+2过去,1回来,3+4过去,2回来,此时时间是

(以下1,2,3,4代指编号为1,2,3,4的人时间,下同)

t1=2+1+4+2

第二种情况:1+4过去,1回来,1+3过去,1回来;也就是通过1来回,逐个过去。时间是

t2=4+1+3+1

由上两种情况,总结出选择哪一种过法,可以比较t1和t2的时间取小,也就是看 2+2>1+3?t1:t2;

AC代码:

#include <iostream>
#include <cstdio>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
long long num[100005];
int n;
long long ans=0;
int solve(int n)
{
    if(n==3) return 1;
    if(n==2) return 2;
    ans+=min(num[2]*2+num[1]+num[n],num[1]*2+num[n]+num[n-1]);
    solve(n-2);
}

int main()
{

    while(scanf("%d",&n)!=EOF)
    {
        ans=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&num[i]);
        }
        sort(num+1,num+n+1);
        if(n==1) ans=num[1];
        else if(solve(n)==1)  ans+=(num[1]+num[2]+num[3]);
        else ans+=num[2];
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Zhidai_/article/details/80448404