ismail5g

Untitled

Apr 7th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int N;
  6. bool isSmall[105];
  7. bool hit[105];
  8. // positions[N] = D
  9. int positions[105];
  10.  
  11. // Will always need to skip over the small rocks at least once
  12. int GetWorstDistance()
  13. {
  14.     int minDist = positions[0];
  15.     for (int i = 0; i < N; ++i)
  16.     {
  17.         hit[i] = true;
  18.         // Skip over first small for now
  19.         if (!isSmall[i + 1])
  20.         {
  21.             minDist = max(minDist, positions[i + 1] - positions[i]);
  22.         }
  23.         else
  24.         {
  25.             minDist = max(minDist, positions[i + 2] - positions[i]);
  26.             ++i;
  27.         }
  28.     }
  29.  
  30.     for (int i = N; i > 0; --i)
  31.     {
  32.         if (!hit[i - 1] || !isSmall[i - 1])
  33.         {
  34.             minDist = max(minDist, positions[i] - positions[i - 1]);
  35.         }
  36.         else
  37.         {
  38.             minDist = max(minDist, positions[i] - positions[i - 2]);
  39.             --i;
  40.         }
  41.     }
  42.     return minDist;
  43. }
  44.  
  45. int main()
  46. {
  47.     int T;
  48.     cin >> T;
  49.  
  50.     for (int t = 1; t <= T; ++t)
  51.     {
  52.         int D;
  53.         cin >> N >> D;
  54.         for (int i = 0; i < N; ++i)
  55.         {
  56.             char type, sep;
  57.             cin >> type >> sep >> positions[i];
  58.             isSmall[i] = (type == 'S');
  59.  
  60.             hit[i] = false;
  61.         }
  62.  
  63.         isSmall[N] = false;
  64.         positions[N] = D;
  65.         hit[N] = false;
  66.  
  67.         cout << "Case " << t << ": " <<
  68.             GetWorstDistance() << '\n';
  69.     }
  70. }
Add Comment
Please, Sign In to add comment