UVa 10440 Ferry Loading II

Source: Internet
Author: User



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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.