fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25. vector<int> a;
  26. void consistency1(int n) {
  27.  
  28. unordered_map<int,int> mp;
  29. int s = 0, e = 0;
  30. int ans = 0;
  31.  
  32. while(e<n){
  33. mp[a[e]]++;
  34.  
  35. while(s<=e && mp.size() < e-s+1 ){
  36. mp[a[s]]--;
  37. if(mp[a[s]] == 0) mp.erase(a[s]);
  38. s++;
  39. }
  40.  
  41. int len = e-s+1;
  42. ans = max(ans, len);
  43.  
  44. e++;
  45. }
  46.  
  47. cout << ans << " ";
  48.  
  49. }
  50.  
  51.  
  52. void consistency2(int n) {
  53.  
  54. unordered_map<int,int> mp;
  55. int s = 0, e = 0;
  56. int ans = 0;
  57.  
  58. while(e<n){
  59. mp[a[e]]++;
  60.  
  61. if(mp.size() == e-s+1){
  62. ans = max(ans, e-s+1);
  63. e++;
  64. }
  65. else{
  66. while(s<=e && mp.size() < e-s+1 ){
  67. mp[a[s]]--;
  68. if(mp[a[s]] == 0) mp.erase(a[s]);
  69. s++;
  70. }
  71. int len = e-s+1;
  72. ans = max(ans, len);
  73.  
  74. e++;
  75. }
  76. }
  77.  
  78. cout << ans << " ";
  79.  
  80. }
  81.  
  82.  
  83. //? Last Occurence Sliding window
  84.  
  85. //* We are trying to maintain a window of unique elements only
  86. //* [ a p k e b c e]
  87. //* So assuming [ a p k e b c ] are distinct elements in window
  88. //* If a[e] disturbs unqiueness => shrink window i.e s = lastPos(a[e]) + 1
  89. //* a p k e [ b c e ]
  90.  
  91.  
  92. //* e [ a b c e]
  93. //* If a[e] disturbs unqiueness => shrink s = max(s , lastPos(a[e]) + 1 ) == s
  94.  
  95.  
  96. void consistency3(int n) {
  97.  
  98. //* ele => index
  99. unordered_map<int,int> mp;
  100. int s = 0, e = 0;
  101. int ans = 0;
  102.  
  103. while(e<n){
  104.  
  105. if(mp.count(a[e])){
  106. int last = mp[a[e]];
  107.  
  108. while(s<=last){
  109.  
  110. //* Since all distinct (ele -> index) stored, so directly remvoe
  111. mp.erase(a[s]);
  112. s++;
  113. }
  114. s = last + 1;
  115. }
  116. ans = max(ans, e-s+1);
  117. mp[a[e]] = e;
  118.  
  119. e++;
  120. }
  121.  
  122. cout << ans << " ";
  123.  
  124. }
  125.  
  126.  
  127. void solve() {
  128.  
  129. int n;
  130. cin >> n;
  131.  
  132. a.resize(n);
  133. for(int i=0; i<n; i++) cin >> a[i];
  134.  
  135. consistency1(n);
  136. consistency2(n);
  137. consistency3(n);
  138. cout << endl;
  139.  
  140. }
  141.  
  142.  
  143.  
  144.  
  145.  
  146. int32_t main() {
  147. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  148.  
  149. int t = 1;
  150. // cin >> t;
  151. while (t--) {
  152. solve();
  153. }
  154.  
  155. return 0;
  156. }
Success #stdin #stdout 0.01s 5288KB
stdin
10
3 2 4 5 2 6 7 8 9 10
stdout
8 8 8