Advertisement
STANAANDREY

itec 2023 pb2

May 6th, 2023
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. /*
  4. int solution(vector<int> &A) {
  5.     int sum = 0, ans = -1;
  6.     const int n = A.size();
  7.     bool isNoNeg = false;
  8.     for (int i = 0; i < n; i++) {
  9.         if (A[i] < 0) {
  10.             ans = max(ans, sum);
  11.             sum = 0;
  12.         } else {
  13.             sum += A[i];
  14.             isNoNeg = true;
  15.         }
  16.     }
  17.     if (!isNoNeg) {
  18.         return -1;
  19.     }
  20.     ans = max(ans, sum);
  21.     return ans;
  22. }//*/
  23.  
  24.  
  25. int getPS(const vector<int> &ps, int i, int j) {
  26.     int r = ps[j];
  27.     if (i - 1 >= 0) {
  28.         r -= ps[i - 1];
  29.     }
  30.     return r;
  31. }
  32. int solution(vector<int> &A) {
  33.     const int n = A.size();
  34.     const int MAXI = 1e4 + 3;
  35.     vector<int> ps(n);
  36.     vector<pair<int, int>> poss(MAXI, make_pair(-1, -1));
  37.     ps[0] = A[0];
  38.     poss[A[0]].first = 0;
  39.     for (int i = 1; i < n; i++) {
  40.         ps[i] = ps[i - 1] + A[i];
  41.         if (poss[A[i]].first == -1) {
  42.             poss[A[i]].first = i;
  43.         } else {
  44.             poss[A[i]].second = i;
  45.         }
  46.     }
  47.     int ans = -1;
  48.     for (int x = 1; x < MAXI; x++) {
  49.         if (poss[x].second != -1) {
  50.             ans = max(ans, getPS(ps, poss[x].first, poss[x].second));
  51.         }
  52.     }
  53.     //cerr << ps[2] << endl;
  54.     return ans;
  55. }//*/
  56.  
  57. int main() {
  58.     vector<int> v(2, 1);v[1]=2;
  59.     cerr << solution(v) << endl;
  60.     return 0;
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement