fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. using ll = long long;
  4. bool csc(ll x, const unordered_set<ll> &st) {
  5. for (auto y : st) {
  6. ll sum = x + y;
  7. if (sum % 2 == 0) {
  8. long long mid = sum / 2;
  9. if (st.count(mid)) return true;
  10. }
  11. }
  12. return false;
  13. }
  14. int maxlen(const vector<long long> &a) {
  15. unordered_set<ll> st;
  16. int l = 0, ans = 0;
  17. for (int r = 0; r < (int)a.size(); r++) {
  18. while (st.count(a[r])) {
  19. st.erase(a[l]);
  20. l++;
  21. }
  22. while (csc(a[r], st)) {
  23. st.erase(a[l]);
  24. l++;
  25. }
  26. st.insert(a[r]);
  27. ans = max(ans, r - l + 1);
  28. }
  29. return ans;
  30. }
  31. int main(){
  32. ll n;
  33. cin >> n;
  34. vector<ll> a(n+1);
  35. for( int i = 1 ; i<= n ; i++ ) cin >> a[i];
  36. cout << maxlen(a);
  37. return 0;
  38. }
  39.  
  40.  
Success #stdin #stdout 0s 5324KB
stdin
7
1 4 7 2 5 9 3
stdout
4