zoukankan      html  css  js  c++  java
  • UVa 10440

    问题:

    一个渡口,有一艘渡船,运汽车到对面,一次能运n辆车,到达对面要t分钟,返回要t分钟,一共来m辆车。

    给出m个数据,是到达渡口的时间。求最短把全部汽车运到对面的最短时间是多少,次数是多少(时间从0算)。

    思路:贪心。

    考虑三种情况(也可以说是两种)

    一:n>m.最短时间为最后一辆车到达渡口的时间+t,次数为1;

    二:n<=m,m恰好整除n。

    三:n<=m,m除以n有余数。

    详细思路代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <algorithm>
    using namespace std;
    
    #define MAXN 1500
    
    int Max(int a,int b){
        if (a>b)
            return a;
        else
            return b;
    }
    
    class Ferry{
        private:
            int n;
            int m;
            int t;
            int cars[MAXN];
        public:
            void process();
    };
    
    void Ferry::process(){
        int cases;//测试样例个数
        int ansTime,ansNum;//结果时间,次数
    
    
        cin >>cases;
        while(cases--){
            ansTime = 0;
            ansNum = 0;
            cin>>n>>t>>m;
            for(int i = 0;i < m;i++){
                cin>>cars[i];
            }
            
            if(n > m){//来的车辆比每次能运的还要少
                ansTime = cars[m-1] + t;
                ansNum = 1;
            }
            else if(m % n == 0){
                for(int i = n - 1;i < m;i += n){
                    ansTime = Max(ansTime,cars[i]);/*比较渡船每次返回(起始也算)渡口的时间和车辆来到渡口的时间,哪个最晚选哪个*/
                    ansTime += 2 * t;
                }
                ansTime = ansTime - t;//最后一次不必返航
                ansNum = m/n;
            }
            else{
                int remainder;//求出总车辆和一次能运车辆的余数
                remainder = m%n;
                ansTime = cars[remainder - 1]+2*t;//先运余数数目的车辆
                for(int i = remainder + n - 1;i < m;i = i + n){/*剩下就是每次装满的情况*/
                    ansTime = Max(ansTime ,cars[i] );
                    ansTime += 2 * t;
                }
                ansTime -=  t;
                ansNum = m/n + 1;
            }
    
            cout<<ansTime<<" "<<ansNum<<endl;
        }
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
            freopen("D:\acm.txt","r",stdin);
        #endif // ONLINE_JUDGE
        Ferry ferry;
        ferry.process();
        return 0;
    }
    Donghua University
  • 相关阅读:
    从搜索引擎角度看SEO
    关键词排名与网站优化有哪三大误区?
    真正提升关键词排名的外链应该怎样发?
    高质量外链的十大特性
    四个方面分析SEO如何提高网站的权重
    Linux(ubuntu)使用dd从iso制作win7安装u盘(读卡器一样),以及备份分区
    折腾slidingmenu
    生命游戏介绍
    21232f297a57a5a743894a0e4a801fc3
    final关键字
  • 原文地址:https://www.cnblogs.com/ohxiaobai/p/4490660.html
Copyright © 2011-2022 走看看