asdfg0998

Untitled

May 4th, 2025
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int countStableSubsegmentsBrute(const vector<int>& capacity) {
  7.     int n = capacity.size();
  8.     int count = 0;
  9.  
  10.     for (int l = 0; l < n; ++l) {
  11.         long long midSum = 0;
  12.         for (int r = l + 1; r < n; ++r) {
  13.             if (r - l >= 2) { // length ≥ 3
  14.                 midSum += capacity[r - 1];
  15.                 if (capacity[l] + capacity[r] == midSum) {
  16.                     count++;
  17.                 }
  18.             } else {
  19.                 midSum += capacity[r - 1];
  20.             }
  21.         }
  22.     }
  23.  
  24.     return count;
  25. }
  26.  
  27. int main() {
  28.     vector<int> capacity = {9, 3, 3, 3, 9};
  29.     cout << "Stable subsegments: " << countStableSubsegmentsBrute(capacity) << endl; // Output: 2
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment