Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <iterator>
- #include <vector>
- #include <set>
- using namespace std;
- bool check_existence_in_vec(int value, const vector<int> &vec)
- {
- return binary_search(vec.begin(), vec.end(), value);
- }
- int main() {
- int n, m;
- cin >> n >> m;
- int insert;
- vector<int> west, east;
- for (int i = 0; i < n; i++) {
- cin >> insert;
- west.push_back(insert);
- }
- for (int i = 0; i < m; i++) {
- cin >> insert;
- east.push_back(insert);
- }
- long duration = 0;
- vector<int> total_time;
- total_time.insert( total_time.end(), west.begin(), west.end() );
- total_time.insert( total_time.end(), east.begin(), east.end() );
- sort( total_time.begin(), total_time.end() );
- // boolean for ferry
- bool stay_at_east = false;
- for (unsigned int i = 0; i < total_time.size(); i++)
- {
- if ( i == 0)
- {
- // ferry is staying at west at the beginning
- // check if the first vehicle is at west or not
- if (check_existence_in_vec( total_time[0], west) )
- {
- // it takes 100 units of time to escort to the east
- duration = total_time[0] + 100;
- stay_at_east = true;
- }
- // the first vehicle is staying at east
- else
- {
- // go to east and escort the vehicle back to west take 200 units
- duration = total_time[0] + 200;
- stay_at_east = false;
- }
- }
- else // the next cases
- {
- if (stay_at_east) // ferry is at east
- {
- if (check_existence_in_vec(total_time[i], east)) // a vehicle is also at east
- {
- if (total_time[i] > duration)
- {
- int delta = total_time[i] - duration;
- duration += delta + 100;
- }
- else duration += 100;
- stay_at_east = false;
- }
- else // a vehicle is at west
- {
- // duration += 100; // tales 100 units to get the west
- if (total_time[i] > duration)
- {
- int delta = total_time[i] - duration;
- duration += delta + 200;
- }
- else duration += 200;
- stay_at_east = true;
- }
- }
- else // ferry is at west
- { if (check_existence_in_vec(total_time[i], west)) // a vehicle is also at west
- {
- if (total_time[i] > duration)
- {
- int delta = total_time[i] - duration;
- duration += delta + 100;
- }
- else duration += 100;
- stay_at_east = true;
- }
- else // a vehicle is at east
- {
- // duration += 100;
- if (total_time[i] > duration)
- {
- int delta = total_time[i] - duration;
- duration += delta + 200;
- }
- else duration += 200;
- stay_at_east = false;
- }
- }
- }
- }
- cout << duration << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement