fork download
  1. # include <bits/stdc++.h>
  2. using namespace std;
  3. int s[1000005];//s[i] multimea unde este elementul i
  4. int v[1000005];//v[i] lungimee lungimii i
  5. int get(int k)
  6. {
  7. return s[k] == k ? k:s[k] = get(s[k]);
  8. }
  9. int main(void)
  10. {
  11. freopen("compact2.in","r",stdin);
  12. int n,x;
  13. scanf("%d",&n);
  14. for (int i = 1;i <= n;++i)
  15. {
  16. scanf("%d",&x);
  17. s[x] = x;
  18. if (s[x+1] && s[x-1]) // nu ma este necesar de facut multimea x deoarece x nu va mai fi accesat doar consideram ca am adaugat la multimea x - 1,x + 1
  19. ++v[get(x+1)],++v[get(x-1)];
  20. else
  21. if (s[x+1])
  22. ++v[s[x] = get(x+1)];
  23. else
  24. if (s[x-1])
  25. ++v[s[x] = get(x-1)];
  26. else v[x] = 1;
  27. }
  28. ofstream fo("compact2.out");
  29. return fo << *max_element(v+1,v+1+n) << '\n',0;
  30. }
Runtime error #stdin #stdout 0s 11264KB
stdin
Standard input is empty
stdout
Standard output is empty