Algorithm


problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1842 

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. A car may arrive at either river bank to be transported by the ferry to the opposite bank. The ferry travels continuously back and forth between the banks so long it is carrying a car or there is at least one car waiting at either bank. Whenever the ferry arrives at one of the banks, it unloads its cargo and loads up to n cars that are waiting to cross. If there are more than n, those that have been waiting the longest are loaded. If there are no cars waiting on either bank, the ferry waits until one arrives, loads it (if it arrives on the same bank of the ferry), and crosses the river. At what time does each car reach the other side of the river? 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), and the bank at which the car arrives (‘left’ or ‘right’). Output For each test case, output one line per car, in the same order as the input, giving the time at which that car is unloaded at the opposite bank. Output an empty line between cases. You may assume that 0 < n, t, m ≤ 10000. The arrival times for each test case are strictly nondecreasing. The ferry is initially on the left bank. Loading and unloading time may be considered to be 0. Sample Input 2 2 10 10 0 left 10 left 20 left 30 left 40 left 50 left 60 left 70 left 80 left 90 left 2 10 3 10 right 25 left 40 left

Sample Output 10 30 30 50 50 70 70 90 90 110 30 40 60

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
using namespace std;

int main() {
    int k;
    scanf("%d",&k);
    int n, t, m;
    int c;
    string side;
    while(k--){
        scanf("%d %d %d\n",&n,&t,&m);
        vector<int> res(m);
        queue < pair<int,int>> left;
        queue < pair<int,int>> right;
        for(int i=0;i < m;i++){
            cin >> c >> side;
            if(side == "left") left.push({c,i});
            else right.push({c,i});
        }
        bool leftNow = true;
        int time = 0;
        while(!left.empty() || !right.empty()){
            // wait for a car to arrive if needed
            int timeMinCar = min(
                    left.empty() ? INT_MAX : left.front().first,
                    right.empty() ? INT_MAX : right.front().first);
            if(time < timeMinCar){
                time = timeMinCar;
            }
            int remain = n;
            if(leftNow){
                while(remain && !left.empty() && left.front().first  < = time){
                    res[left.front().second] = time+t;
                    left.pop();
                    remain--;
                }
            } else {
                while(remain && !right.empty() && right.front().first  < = time){
                    res[right.front().second] = time+t;
                    right.pop();
                    remain--;
                }
            }
            time += t;
            leftNow = !leftNow;
        }
        for(auto& i : res) cout << i << endl;
        if(k> cout << endl;
    }
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
2
2 10 10
0 left
10 left
20 left
30 left
40 left
50 left
60 left
70 left
80 left
90 left
2 10 3
10 right
25 left
40 left

Output

x
+
cmd
10
30
30
50
50
70
70
90
90
110
30
40
60
Advertisements

Demonstration


UVA Online Judge solution - 10901-Ferry Loading III - UVA Online Judge solution in C,C++,java

Previous
UVA Online Judge solution - 10895-Matrix Transpose - UVA Online Judge solution in C,C++,java
Next
UVA Online Judge solution - 10905-Children's Game - UVA Online Judge solution in C,C++,java