POJ 1700 cross river (数学模拟)

来源:互联网 发布:女鞋淘宝店铺简介 编辑:程序博客网 时间:2024/06/08 09:27

                                                                                                                          Crossing River
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 10311 Accepted: 3889

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

141 2 5 10

Sample Output

17


思路:(假如 1-n个人时间time[n],递增排列)
  • 只有一个人的时候:sum=time[1];
  • 二个人的时候:       sum=time[1]+time[2]
  • 三的人的时候:       sum=time[1]+time[2]+time[3]
  • 重点!当大于四个人的时候,为了追求时间最短化,只有两种运送方式:(1) 最快,次快去-->最快回--->最慢,次慢去-->次快   time[2]+time[1]+time[n]+time[n-1]+time[2]
  •                                                                                                          (2) 最快,最慢去-->最快回-->最快,次快去-->最快回     time[n]+time[1]+time[n-1]+time[1]

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


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 木头壶盖有异味怎么办 挎包拉链坏了怎么办 斜挎包没有拉链怎么办 树脂补牙没抛光怎么办 猪拉稀不吃食怎么办 亚麻衣服刺皮肤怎么办 自热米饭不熟怎么办 孕妇用了微波炉怎么办 蛋挞变软了怎么办 外卖炒面坨了怎么办 手机发热充电慢怎么办 饭盒盖子松了怎么办 饭盒盖子盖不住怎么办 饭盒盖子吸不住怎么办 饭盒盖子变形了怎么办 饭盒盖子凹进去怎么办 饭盒盖吸不住怎么办 饭盒微波炉加热后打不开怎么办 微波炉加热饭盒打不开怎么办 塑料饭盒加热后打不开怎么办 真空锅锅盖打不开怎么办 玻璃真空水壶打不开怎么办 保温饭盒盖子打不开怎么办 饭盒盖章松了怎么办 电压力锅卡住了怎么办 铁的饭盒打不开怎么办 微波炉饭盒盖子打不开怎么办 微波炉盖子吸住了怎么办 剩下的糯米饭怎么办 饭盒微波加热打不开怎么办 微波炉触屏不灵怎么办 微波炉旋钮坏了怎么办 微波炉蒸馒头硬怎么办 小猫两天不吃饭怎么办 减肥吃了巧乐兹怎么办 孕妇吃了点蟹籽怎么办 做寿司没有肉松怎么办 做寿司没有海苔怎么办 孩子不爱吃早餐怎么办 早上不想吃早餐怎么办 早上来不及吃早餐怎么办