貪心之 Ferry Loading II (POJ 2336)

來源:互聯網
上載者:User

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

22 10 1001020304050607080902 10 3103040

Sample Output

100 550 2

#include <iostream>using namespace std;int k;int main() {cin >> k;int n,t,m;while(k--){cin >> n >> t >> m;int a = m % n;int times = m / n; //運的次數.  直接相除即可.if(a!=0)times++;int minTime=0;int last;int i=0;//最短時間只和最後一隻船有關係. 如果 m%n==0 則每次都運n個,必是最優。//否則,第一次運 m%n個, 可保證最優。if(a){//如果第一次運 m%n個for(i=0; i<a; i++)cin >> last;minTime = last +  2*t;}int cnt = 0;while(i < m){cin >> last;cnt ++;if(cnt == n){if(last > minTime){minTime = last + 2*t;}else{minTime += 2*t;}cnt = 0;}i++;}minTime -= t; //最後一次不用返回cout << minTime << " " << times << endl;}return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.