Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <exception>
  5.  
  6. template <class Int> void solve(const std::vector<Int>& vec) {
  7.     using namespace std;
  8.     size_t start{0}, mx_start{0};
  9.     size_t finish{0}, mx_finish{0}; // included [0; size(vec)-1]
  10.     size_t mx_sz{1};
  11.  
  12.     auto same_sign = [&vec](size_t cur_ind) -> bool {
  13.         if (cur_ind == 0) throw runtime_error("Tried to acces vec[-1]");
  14.         if (cur_ind == size(vec)) throw runtime_error("Tried to acces vec[size(vec)]");
  15.         return (vec[cur_ind - 1] >= 0 && vec[cur_ind] >= 0) || (vec[cur_ind - 1] < 0 && vec[cur_ind] < 0);
  16.     };
  17.  
  18.     for (size_t i{1}; i < size(vec); ++i) {
  19.         if (same_sign(i)) {
  20.             finish++;
  21.         } else {
  22.             size_t sz = 1 + finish - start;
  23.             if (sz > mx_sz) {
  24.                 mx_sz = sz;
  25.                 mx_finish = finish;
  26.                 mx_start = start;
  27.             }
  28.             start = finish = i;
  29.             sz = 1;
  30.         }
  31.     }
  32.     mx_finish++;
  33.     while (mx_start != mx_finish) {
  34.         cout << vec[mx_start++] << ' ';
  35.     }
  36.     cout << '\n';
  37. }
  38.  
  39. signed main(signed argc, char* argv[]) {
  40.     using namespace std;
  41.     size_t sz;
  42.     cin >> sz;
  43.     vector<int> vec(sz);
  44.     for (auto& elem : vec) cin >> elem;
  45.     solve(vec);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement