Advertisement
tepyotin2

cowvid19

Dec 31st, 2023
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int N;
  5. vector<int> bales;
  6.  
  7. int exploded_num(int start, int direction) {
  8.     int radius = 1;
  9.     int prev = start;
  10.     while (true) {
  11.         int next = prev;
  12.         // Get the furthest explosion index without exceeding the current radius
  13.         while (next + direction >= 0 && next + direction < N &&
  14.                abs(bales[next + direction] - bales[prev]) <= radius) {
  15.             next += direction;
  16.         }
  17.  
  18.         // We didn't find a new haybale, so the chain explosion is over
  19.         if (next == prev) { break; }
  20.  
  21.         // Update our previous explosion and increment radius
  22.         prev = next;
  23.         radius++;
  24.     }
  25.     return abs(prev - start);
  26. }
  27.  
  28. int main() {
  29.     freopen("cowvid.in", "r", stdin);
  30.     //freopen("angry.out", "w", stdout);
  31.  
  32.     cin >> N;
  33.     bales.resize(N);
  34.     for (int i = 0; i < N; i++) { cin >> bales[i]; }
  35.  
  36.     sort(bales.begin(), bales.end());
  37.     int max_exploded = 0;
  38.     for (int i = 0; i < N; i++) {
  39.         // Get the number of exploded bales for the left & right side
  40.         max_exploded =
  41.             max(max_exploded, exploded_num(i, -1) + exploded_num(i, 1) + 1);
  42.     }
  43.     cout << max_exploded << endl;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement