tuki2501

twosum.cpp

Nov 7th, 2021
1,108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. template<typename T>
  5. void chmax(T &a, T b) {
  6.   if (a < b) a = b;
  7. }
  8.  
  9. int main() {
  10.   int n; cin >> n;
  11.   vector<int> a(n + 1), s(n + 1);
  12.   for (int i = 1; i <= n; i++) {
  13.     cin >> a[i];
  14.     s[i] = a[i] + s[i - 1];
  15.   }
  16.   int ans = 0;
  17.   unordered_map<int,int> f; f[0] = 0;
  18.   for (int i = 1; i <= n; i++) {
  19.     for (int j = i + 1; j <= n; j++) {
  20.       int sum = s[j] - s[i];
  21.       if (f.count(s[i] - sum)) {
  22.         chmax(ans, j - f[s[i] - sum]);
  23.       }
  24.     }
  25.     if (!f.count(s[i])) f[s[i]] = i;
  26.   }
  27.   cout << ans << '\n';
  28. }
Advertisement
Add Comment
Please, Sign In to add comment