zoukankan      html  css  js  c++  java
  • POJ 1700

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 13982   Accepted: 5349

    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

    1
    4
    1 2 5 10
    

    Sample Output

    17


    贪心思想(一般都是先排序)
    每次从此岸到对岸移动的两个人要么这两个人中有一个是时间最快的那个人,要么这两个人到达对岸后再也不回来。即:要么最快+最慢(最快回来换人),要么最慢+次慢(不回来)。
    1.对N个人过河时间从小到大排序。p[i];
    2.分情况讨论:
    ⑴当n = 1,直接过河。sum = p[1]
    (2)当n = 2,直接过河。 sum = p[2]
    (3)当n = 3,无论怎么过河, sum = p[1] + p[2] + p[3] 
    (4)当n >= 4,设从小到大排序后位a,b,……,c,d,大于4个人,a,b是最小的两个人,c,d是最大的两个人,目标就是把最大的两个人送过去。就要牺牲最小的。
    用最小的来送:A=d + a + c + a = 2a + c + d = 2*p[1] + p[end-1] + p[end];(a,d过去,a回来,a,c过去,a回来)
    两小送两大:B= b + b + d + a = a + 2b + d = 2*p[1] + p[end-1] + p[end];(a,b过去,b回来,c,d过去,a回来)
    循环:sum = min(A,B),直到n <= 3 时候结束。



    刚开始蠢,用函数递归调用,果断超时……

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 int p[1003];
     5 int min(int a,int b) {return a<b?a:b;}
     6 int time_sum(const int p[],int end)
     7 {
     8     if(end == 3) return p[1]+p[2]+p[3];
     9     if(end == 2) return p[2];
    10     if(end == 1) return p[1];
    11     int time_1= 2*p[1] + p[end-1] + p[end] + time_sum(p,end-2);
    12     int time_2= 2*p[2] + p[end] + p[1] + time_sum(p,end-2);
    13     return min(time_1,time_2);
    14 }
    15 int main()
    16 {
    17     int t,n,time;
    18     scanf("%d",&t);
    19     while(t--){
    20         scanf("%d",&n);
    21         for(int i=1;i<=n;i++){
    22             scanf("%d",&p[i]);
    23         }
    24         sort(p+1,p+n+1);
    25         if(n == 1){
    26             time=p[1];
    27         }
    28         else if(n == 2){
    29             time=p[2];
    30         }
    31         else if(n == 3){
    32             time=p[1]+p[2]+p[3];
    33         }
    34         else if(n >= 4){
    35             time=time_sum(p,n);
    36         }
    37         printf("%d
    ",time);
    38     }
    39 }


    后来改了下就好了:

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 int p[1003];
     5 int min(int a,int b) {return a<b?a:b;}
     6 int main()
     7 {
     8     int t,n,time;
     9     scanf("%d",&t);
    10     while(t--){
    11         scanf("%d",&n);
    12         for(int i=1;i<=n;i++){
    13             scanf("%d",&p[i]);
    14         }
    15         sort(p+1,p+n+1);
    16         if(n == 1){
    17             time=p[1];
    18         }
    19         else if(n == 2){
    20             time=p[2];
    21         }
    22         else if(n == 3){
    23             time=p[1]+p[2]+p[3];
    24         }
    25         else if(n >= 4){
    26             time=0;
    27             int end=n;
    28             while(end >= 4){
    29                 time+=min( 2*p[1] + p[end-1] + p[end] , 2*p[2] + p[end] + p[1] );
    30                 end-=2;
    31             }
    32             if(end == 3) time+=p[1]+p[2]+p[3];
    33             else if(end == 2) time+=p[2];
    34             else if(end == 3) time+=p[1];
    35         }
    36         printf("%d
    ",time);
    37     }
    38 }
  • 相关阅读:
    amaze(妹子~) 好像挺好玩
    php 获取图片base64编码格式数据
    一些laravel博文
    微信移动端(wap)开发调试工具
    深入理解控制反转(IoC)和依赖注入(DI)
    使用 composer 下载更新卸载类库
    ionic ui 框架
    laravel 添加 404 页面
    laravel 调试模式及日志配置
    iOS-方法之+ initialize 与 +load
  • 原文地址:https://www.cnblogs.com/dilthey/p/6804175.html
Copyright © 2011-2022 走看看