Title 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 is 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, 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's the earliest time and all the cars can be transported across the river? What's the minimum number of trips that the operator must do to deliver all cars by that time?
Input
The first line of input contains C, the number of test cases. Each test case is begins with N, T, M. M lines follow, each giving the arrival time to 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 has arrived up to that time.
Output
For each test case, output a single line with integers:the time, in minutes since the beginning of the Last car was 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.
Assume that 0 < n, T, M < 1440. The arrival times for each test case is in non-decreasing order.
Sample Input
22 10 1001020304050607080902) 10 3103040
Sample Output
100 550 2
Main topic
The ferry ships every time n boats, once a river times T. A given M-boat and its timetable for arriving at the ferry. The earliest delivery time and the minimum number of times.
Thinking of solving problems
- Total time = Last vehicle arrival time + last car waiting time + one-way time
- The problem translates to the minimum waiting time for the last car
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
bool cmp(int a, int b)
{
return a > b;
}
int a[1500];
int main()
{
ios::sync_with_stdio(false);
//freopen("date.in", "r", stdin);
//freopen("date.out", "w", stdout);
int N = 0, n = 0, t = 0, m = 0,count=0,time=0;
cin >> N;
for (int i = 0; i < N; i++)
{
count = 0;
time = 0;
cin >>n>> t >> m;
for (int j = 1; j <= m; j++)
{
cin>>a[j];
}
//sort(a, a + n, cmp);
if (n >= m)
{
count = 1;
time = a[m]+2 * t;
}
else
{
if (m%n==0)
{
count = m / n;
for (int l = n; l <= m; l += n)
{
time = max(a[l], time);
time += (2 * t);
}
}
else {
count = m / n + 1;
time = a[m%n] + 2 * t;
//time = a[1] + 2 * t;
for (int k = m%n + n; k <= m; k += n)
{
time = max(a[k], time);
time += (2 * t);
}
}
}
cout << time-t << " " << count << endl;
}
return 0;
}
From for notes (Wiz)
Sdau Course Exercises--problemo (1014)