zoukankan      html  css  js  c++  java
  • 过河问题nyoj47

     

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:5
     
    描述

    在漆黑的夜里,N位旅行者来到了一座狭窄而且没有护栏的桥边。如果不借助手电筒的话,大家是无论如何也不敢过桥去的。不幸的是,N个人一共只带了一只手电筒,而桥窄得只够让两个人同时过。如果各自单独过桥的话,N人所需要的时间已知;而如果两人同时过桥,所需要的时间就是走得比较慢的那个人单独行动时所需的时间。问题是,如何设计一个方案,让这N人尽快过桥。 

     
    输入
    第一行是一个整数T(1<=T<=20)表示测试数据的组数
    每组测试数据的第一行是一个整数N(1<=N<=1000)表示共有N个人要过河
    每组测试数据的第二行是N个整数Si,表示此人过河所需要花时间。(0<Si<=100)
    输出
    输出所有人都过河需要用的最少时间
    样例输入
    1
    4
    1 2 5 10
    样例输出
    17
    #include <stdio.h>
    #include <stdlib.h>
    #define M 1001
    int time[M];
    
    int cmp(const void *a, const void *b)
    {
        return *(int *)a - *(int *)b;
    }
    
    int inmp(int a, int b)
    {
        return a <= b ? a : b;
    }
    
    int fan(int x)
    {
        int sum = 0;
        if(x == 1)
            sum = time[1];/*一个人*/
        else if(x == 2)
            sum = time[2];/*2人一起过河,时间是最慢的那个*/
        else if(x == 3)
            sum = time[3] + time[2] + time[1];/*3人就是加起来*/
        else
        //    sum = fan(x - 2) + 2 * time[2] + time[1] + time[x];/*没有比先让快的过去再让快的负责递手电快的多*//#1
        {
            sum = fan(x - 2) + inmp(time[x]+time[2]*2+time[1], time[x]+time[x - 1] +time[1]*2);//注意‘,’右边的time[1]要乘以2,意思是速度最快的还要返回去*/
        }    
        return sum;    
    }
    
    int main()
    {
        int n, num, i, min_t;
        scanf("%d", &n);
        while(n--){
    
                scanf("%d", &num);
                for(i = 1; i <= num; i++){
    
                        scanf("%d", &time[i]);
    
                }
                qsort(time + 1, num, sizeof(time[0]), cmp);
                min_t = fan(num);
                printf("%d
    ", min_t);
    
        }
        return 0;
    }
    View Code

    #1:我错了,如果第二个人与被接送的人的耗时相差很少,那么选择我说的方案就不够优秀,所以二者都要考虑

  • 相关阅读:
    例题6-8 Tree Uva548
    例题6-7 Trees on the level ,Uva122
    caffe Mac 安装
    Codeforces Round #467 (Div. 1) B. Sleepy Game
    Educational Codeforces Round37 E
    Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons
    Good Bye 2017 E. New Year and Entity Enumeration
    Good Bye 2017 D. New Year and Arbitrary Arrangement
    Codeforces Round #454 D. Seating of Students
    浙大紫金港两日游
  • 原文地址:https://www.cnblogs.com/the-one/p/3260976.html
Copyright © 2011-2022 走看看