hdoj--A Heavy Rainy Day(贪心好题)

159 篇文章 0 订阅

A Heavy Rainy Day

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 475   Accepted Submission(s) : 124
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

As you know, Hangzhou always rains heavily, especially in Xiaoheshan area, which does annoy ACMers for a longer time. One day, N hardworking ACMers were playing League of Legends in the lab, of course not include me, while it was raining cats and dogs. Everyone wanted to come back to the department where they were living together, but no one wanted to be soaked without umbrella. Unfortunately, they only had one umbrella, which is too small to take more than two individuals. The time that everyone needs to spent to go to the department is known. If two guys came back together, it will take the longer time of this two guys. The question is the earliest time they come back to the department.

Input

The first line contains an integer T(1<=T<=20),which is the number of test cases.
For each testcase:
The first line contains one integer N ,1<=N<=1000, present N ACMers want to come back,
The second line contains N integers Si, present the time the ith ACMers need.

Output

The shortest time they need to come to the department.

Sample Input

1
4
1 2 5 10

Sample Output

17

Author

bytelin

一群逗比被困在实验室,要回寝室,但是此时下起了雨,他们只有一把伞,这把伞每次只能让两个人用,所以需要有两个人先回去,然后来个人

把伞送回,然后再回去俩人,循环往复,直到所有的人都回去,但是人呢都是有差距的,有的人走的快,有的人走得慢,两个人一起回去的时候

所花的时间是根据慢的那个人,求最短的时间。

解题思路;

一般都会让一个跑的最快的人跑来跑去送伞,但是样例说明,并不都是这样的,我们让1,2先走,然后1回去,花费3,然后5,10走,2回来,

花费:15,然后1,2走,花费17!!!!!的确挺坑的,,,如果我们让跑的最快的人跑来跑去,5,10,都回去的时候花费:17,所以还是上

一种方案省时间,这就是规律了,要么最快的两个人,最慢的两个人合作(四个人),要么最快的一个人跟最慢的两个人合作(三个人),每

次想办法送走两个人,比较两种方法的优劣,进行取舍。

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int num[1010];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        scanf("%d",&num[i]);
        sort(num,num+n);
        int i=0,sum=0;
        if(n==3)
        sum=num[2]+num[1]+num[0];
        else if(n==1) sum+=num[0];
        else if(n==2) sum+=num[1];
        else
        {
            while(n>3)
            {
                if(num[0]+2*num[1]+num[n-1]>num[n-1]+num[n-2]+2*num[0])
                sum+=num[n-1]+num[n-2]+2*num[0];
                else
                sum+=num[0]+2*num[1]+num[n-1];
                n-=2;
            }
            if(n==1) sum+=num[0];
            else if(n==2) sum+=num[1];
            else if(n==3) sum+=num[0]+num[1]+num[2]; 
        }
        printf("%d\n",sum);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值