poj 2336 Ferry Loading II—— 过河问题 贪心

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 crosses the river, and the cars exit from the other end of the ferry.
There is a ferry across the river that can take n cars across the river in t minutes and return in t minutes. m cars arrive at the ferry terminal by a given schedule. What is the earliest time that all the cars can be transported across the river? What is the minimum number of trips that the operator must make to deliver all cars by that time?
Input

The first line of input contains c, the number of test cases. Each test case begins with n, t, m. m lines follow, each giving the arrival time for a car (in minutes since the beginning of the day). The operator can run the ferry whenever he or she wishes, but can take only the cars that have arrived up to that time.
Output

For each test case, output a single line with two integers: the time, in minutes since the beginning of the day, when the last car is delivered to the other side of the river, and the minimum number of trips made by the ferry to carry the cars within that time.

You may assume that 0 < n, t, m < 1440. The arrival times for each test case are in non-decreasing order.
Sample Input

2
2 10 10
0
10
20
30
40
50
60
70
80
90
2 10 3
10
30
40

Sample Output

100 5
50 2

题意: 有m辆车要过河,这m辆车是根据时间顺序进入渡口的,而渡口的船每次只能运送n辆车到对岸,且花费时间为t。问:最少需要渡几次才能把这m辆车全部送到对岸,且时间最小。

题解: 时间最小就是最后一辆车到达渡口,而且刚好船又停在渡口,这时候把船渡过去的时间花费就是最后一辆车进渡口的时间加上船运送的时间。因此最优解即是最后一辆车能够最早被运送:

解法:如果 m%n==0 则每次都运送n辆车,否则,第一次运送 m%n 辆车,然后接下来都运送n辆车。

c++ AC 代码

#include<iostream>
#include<cstdio>
const int N = 1445;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,t,m;
        scanf("%d%d%d",&n,&t,&m);
        int L = m%n, k = m/n;
        int return_time = 0,now_time,tr_time = k;
        if(L)
        {
            tr_time++;
            for (int i = 0; i < L; i++) scanf("%d",&now_time);
            return_time += now_time + 2*t;
        }
        
        for (int i = 0; i < k; i++)
        {
            for(int j=0;j<n;j++)
                scanf("%d",&now_time);
            if(return_time > now_time) return_time += 2*t;
            else return_time = now_time + 2*t;
        }
        printf("%d %d\n",return_time-t,tr_time);
        
    }
    return 0;
}

文章参考自:q(≧▽≦q)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值