Problem:
A ferry, there is a ferry, transport cars to the opposite, can transport n vehicles, to the opposite to T minutes, return to T minutes, altogether to M car.
Given m data, is the time to reach the ferry. The shortest time to get all the cars to the opposite side is the shortest, the number of times is how much (time from 0).
Idea: Greed.
Consider three types of situations (or two).
One: N>m. The shortest time for the last car to reach the ferry time +t, the number of times is 1;
Two: N<=m,m exactly divide n.
Three: N<=m,m divided by N.
Detailed Idea code:
#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; // Number of test cases
int ansTime, ansNum; // Result time, times
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) {// There are fewer vehicles coming than each time
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]); / * Compare the time at which the ferry returns (the start is counted) and the time when the vehicle arrives at the ferry, which one is the latest? /
ansTime + = 2 * t;
}
ansTime = ansTime-t; // The last time you do not have to return
ansNum = m / n;
}
else {
int remainder; // Find the remainder of the total vehicle and one capable vehicle
remainder = m% n;
ansTime = cars [remainder-1] + 2 * t; // The number of vehicles to be shipped first
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;
}
UVa 10440 Ferry Loading II