fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool ok(const vector<long long>& v, long long x) {
  5. int m = v.size();
  6. for (int i = 0; i < m; i++) {
  7. for (int j = i + 1; j < m; j++) {
  8. long long a = v[i], b = v[j];
  9. // ba khả năng tạo CSC
  10. if (a + b == 2 * x) return false;
  11. if (a + x == 2 * b) return false;
  12. if (b + x == 2 * a) return false;
  13. }
  14. }
  15. return true;
  16. }
  17.  
  18. int main() {
  19. ios::sync_with_stdio(false);
  20. cin.tie(nullptr);
  21.  
  22. int n;
  23. cin >> n;
  24. vector<long long> a(n);
  25. for (int i = 0; i < n; i++) cin >> a[i];
  26.  
  27. unordered_set<long long> st;
  28. vector<long long> cur;
  29.  
  30. int L = 0, best = 0, bestL = 0;
  31.  
  32. for (int R = 0; R < n; R++) {
  33. // đảm bảo không trùng
  34. while (st.count(a[R])) {
  35. st.erase(a[L]);
  36. cur.erase(find(cur.begin(), cur.end(), a[L]));
  37. L++;
  38. }
  39.  
  40. // đảm bảo không tạo CSC
  41. while (!ok(cur, a[R])) {
  42. st.erase(a[L]);
  43. cur.erase(find(cur.begin(), cur.end(), a[L]));
  44. L++;
  45. }
  46.  
  47. st.insert(a[R]);
  48. cur.push_back(a[R]);
  49.  
  50. if ((int)cur.size() > best) {
  51. best = cur.size();
  52. bestL = L;
  53. }
  54. }
  55.  
  56. cout << best << "\n";
  57. cout << bestL + 1 << " " << bestL + best << "\n";
  58. return 0;
  59. }
Success #stdin #stdout 0.01s 5312KB
stdin
7
1 4 7 2 5 9 3
stdout
4
2 5