UVa 10440 Ferry Loading II

问题:

一个渡口,有一艘渡船,运汽车到对面,一次能运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;
}
时间: 2024-10-06 13:44:28

UVa 10440 Ferry Loading II的相关文章

poj-2336 Ferry Loading II(dp)

题目链接: Ferry Loading II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3946   Accepted: 1985 Description Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a

POJ 2336 Ferry Loading II

POJ 2336 Ferry Loading II 贪心 || 伪dp Description:NULL ( 233333因为我懒 ) 哦..这个解题报告是我从lzy自带解题报告的代码里摘来的233 最早到达对岸的时间,取决于最后一辆车的被运送时间最优解是最后一辆车能够被尽早的运走if(a > dp[i - j]) dp[i] = min(dp[i], 2 * t + a);else dp[i] = min(dp[i], 2 * t + dp[i - j]); dp[i]表示第i个车到达对岸花的

POJ2336 Ferry Loading II 贪心动规

题意:有m辆车,每次最多运n辆过河,过河过去需要t时间回来需要t时间,m辆车一开始并不是都在岸边的,给出m辆车抵达岸边的时间(只有车抵达河岸才能过河),问使得所有车辆过河所需要的最少次数 跟 最早时间 分析: 一开始看题目可能觉得有两个最优解,最少次数跟最早时间,次数最少猜测一下,m%n==0则刚好为m/n次 否则 为m/n+1次,然后考虑最早时间,时间要最早 其实就是最后一辆车最早过河了即可,那么最后一辆车尽早被送走就好,看样子可以贪心 举了几个案例试了一下,若m%n==0则不用说了,每次运n

[POJ2336]Ferry Loading II

题目描述 Description Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry

UVA 10869 - Brownie Points II(树状数组)

UVA 10869 - Brownie Points II 题目链接 题意:平面上n个点,两个人,第一个人先选一条经过点的垂直x轴的线,然后另一个人在这条线上穿过的点选一点作垂直该直线的线,然后划分出4个象限,第一个人得到分数为1,3象限,第二个人为二四象限,问第一个个人按最优取法,能得到最小分数的最大值,和这个值下另一个人的得分可能情况 思路:树状数组,可以枚举一点,如果能求出右上和左下点的个数就好办了,其实用一个树状数组,把y坐标离散化掉,然后记录进来,然后把点按x从左往右,每次删掉点后查询

POJ 3282 Ferry Loading IV(模拟,队列)

题意   汽车通过渡船过河  渡船开始在左边   输入按车辆来的顺序输入河两岸的车   渡船每次运输的汽车的总长度不能超过渡船自己本身的长度  先来的车先走   求轮船至少跨河多少次才能将所有的车辆都运完 简单模拟  建两个队列  分别装左边的车  和右边的车   算出两边各至少需要运输多少次就行了 #include<cstdio> #include<cstring> #include<queue> using namespace std; int main() { i

UVA 11426 - GCD - Extreme (II) (数论)

UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N,求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gcd(1, n) + gcd(2, n) + ... + gcd(n - 1, n).这样的话,就可以得到递推式S(n) = f(2) + f(3) + ... + f(n) ==> S(n) = S(n - 1) + f(n);. 这样问题变成如何求f(n).设g(n, i),表示满足gcd(x, n)

POJ 3282 Ferry Loading IV(简单模拟)

Description Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry cross

POJ 3282 Ferry Loading IV(模拟)

Description Before bridges were common, ferries were used to transport cars across rivers. River ferries, unlike their larger cousins, run on a guide line and are powered by the river's current. Cars drive onto the ferry from one end, the ferry cross