Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <iterator>
  4. #include <vector>
  5. #include <set>
  6. using namespace std;
  7.  
  8. bool check_existence_in_vec(int value, const vector<int> &vec)
  9. {
  10.     return binary_search(vec.begin(), vec.end(), value);
  11. }
  12.  
  13. int main() {
  14.     int n, m;
  15.     cin >> n >> m;
  16.  
  17.     int insert;
  18.     vector<int> west, east;
  19.     for (int i = 0; i < n; i++) {
  20.         cin >> insert;
  21.         west.push_back(insert);
  22.     }
  23.     for (int i = 0; i < m; i++) {
  24.         cin >> insert;
  25.         east.push_back(insert);
  26.     }
  27.  
  28.     long duration = 0;
  29.  
  30.     vector<int> total_time;
  31.     total_time.insert( total_time.end(), west.begin(), west.end() );
  32.     total_time.insert( total_time.end(), east.begin(), east.end() );
  33.  
  34.     sort( total_time.begin(), total_time.end() );
  35.  
  36.     // boolean for ferry
  37.     bool stay_at_east = false;
  38.  
  39.     for (unsigned int i = 0; i < total_time.size(); i++)
  40.     {
  41.         if ( i == 0)
  42.         {
  43.             // ferry is staying at west at the beginning
  44.             // check if the first vehicle is at west or not
  45.             if (check_existence_in_vec( total_time[0], west) )
  46.             {
  47.                 // it takes 100 units of time to escort to the east
  48.                 duration = total_time[0] + 100;
  49.                 stay_at_east = true;
  50.             }
  51.             // the first vehicle is staying at east
  52.             else
  53.             {
  54.                 // go to east and escort the vehicle back to west take 200 units
  55.                 duration = total_time[0] + 200;
  56.                 stay_at_east = false;
  57.             }
  58.         }
  59.         else // the next cases
  60.         {
  61.             if (stay_at_east) // ferry is at east
  62.             {
  63.                 if (check_existence_in_vec(total_time[i], east)) // a vehicle is also at east
  64.                 {
  65.                     if (total_time[i] > duration)
  66.                     {
  67.                         int delta = total_time[i] - duration;
  68.                         duration += delta + 100;
  69.                     }
  70.                     else duration += 100;
  71.                     stay_at_east = false;
  72.                 }
  73.                 else // a vehicle is at west
  74.                 {
  75.                     // duration += 100; // tales 100 units to get the west
  76.                     if (total_time[i] > duration)
  77.                     {
  78.                         int delta = total_time[i] - duration;
  79.                         duration += delta + 200;
  80.                     }
  81.                     else duration += 200;
  82.                     stay_at_east = true;
  83.                 }
  84.             }
  85.             else // ferry is at west
  86.             {  if (check_existence_in_vec(total_time[i], west)) // a vehicle is also at west
  87.                 {
  88.                     if (total_time[i] > duration)
  89.                     {
  90.                         int delta = total_time[i] - duration;
  91.                         duration += delta + 100;
  92.                     }
  93.                     else duration += 100;
  94.                     stay_at_east = true;
  95.                 }
  96.                 else // a vehicle is at east
  97.                 {
  98.                     // duration += 100;
  99.                     if (total_time[i] > duration)
  100.                     {
  101.                         int delta = total_time[i] - duration;
  102.                         duration += delta + 200;
  103.                     }
  104.                     else duration += 200;
  105.                     stay_at_east = false;
  106.                 }
  107.             }
  108.         }
  109.     }
  110.  
  111.     cout << duration << endl;
  112.  
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement