Advertisement
a53

Interesting Array

a53
Nov 19th, 2020
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3.  
  4. using namespace std;
  5.  
  6. const int NMAX = 1e5;
  7. int N, a[NMAX + 2];
  8. long long sol = 1LL;
  9.  
  10. void Solve(int L, int R)
  11. {
  12. unordered_map <int, int> fr;
  13.  
  14. for(int i = L; i <= R; i++)
  15. fr[a[i]]++;
  16.  
  17. int maxFq = 0, minFq = R - L + 2;
  18.  
  19. for(auto it : fr)
  20. {
  21. maxFq = max(maxFq, it.second);
  22. minFq = min(minFq, it.second);
  23. }
  24.  
  25. sol = max(sol, 1LL * maxFq * minFq);
  26.  
  27. int l = -1;
  28. for(int i = L; i <= R; i++)
  29. if(fr[a[i]] == minFq)
  30. {
  31. if(l != -1)
  32. {
  33. Solve(l, i - 1);
  34. l = -1;
  35. }
  36. }
  37. else
  38. {
  39. if(l == -1)
  40. l = i;
  41. }
  42.  
  43. if(l != -1)
  44. Solve(l, R);
  45. }
  46.  
  47. int main()
  48. {
  49. ios_base::sync_with_stdio(false);
  50. cin.tie(0);
  51. cout.tie(0);
  52.  
  53. cin >> N;
  54. for(int i = 1; i <= N; i++)
  55. cin >> a[i];
  56.  
  57. Solve(1, N);
  58.  
  59. cout << sol << '\n';
  60.  
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement