fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. vector<int> a;
  31.  
  32. //* Template 1 :
  33.  
  34. int consistency1(int n){
  35.  
  36. unordered_map<int,int> mp;
  37.  
  38. int s = 0, e = 0;
  39. int distinct = 0;
  40.  
  41. int ans = 0;
  42.  
  43. while(e<n){
  44. mp[a[e]]++;
  45. if(mp[a[e]] == 1) distinct++;
  46.  
  47. if(distinct == e-s+1){
  48. ans = max(ans, e-s+1);
  49. e++;
  50. }
  51. else{
  52. while(s<=e && distinct < e-s+1){
  53. mp[a[s]]--;
  54. if(mp[a[s]] == 0){
  55. distinct--;
  56. mp.erase(a[s]);
  57. }
  58. s++;
  59. }
  60. ans = max(ans, e-s+1);
  61. e++;
  62. }
  63.  
  64. }
  65.  
  66. return ans;
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. //* Template 2 :
  76.  
  77. int consistency2(int n){
  78.  
  79. unordered_map<int,int> mp;
  80.  
  81. int s = 0, e = 0;
  82. int distinct = 0;
  83.  
  84. int ans = 0;
  85.  
  86. while(e<n){
  87. mp[a[e]]++;
  88. if(mp[a[e]] == 1) distinct++;
  89.  
  90. while(s<=e && distinct < e-s+1){
  91. mp[a[s]]--;
  92. if(mp[a[s]] == 0){
  93. distinct--;
  94. mp.erase(a[s]);
  95. }
  96. s++;
  97. }
  98. ans = max(ans, e-s+1);
  99. e++;
  100.  
  101. }
  102.  
  103. return ans;
  104. }
  105.  
  106.  
  107.  
  108.  
  109.  
  110. //* Maintaining Trick
  111.  
  112. int consistency3(int n){
  113.  
  114. unordered_map<int,int> mp;
  115.  
  116. int s = 0, e = 0;
  117.  
  118. int ans = 0;
  119.  
  120. while(e<n){
  121.  
  122. while(s<=e && mp.count(a[e])){
  123. mp[a[s]]--;
  124. if(mp[a[s]] == 0) mp.erase(a[s]);
  125. s++;
  126. }
  127. mp[a[e]]++;
  128. ans = max(ans, e-s+1);
  129. e++;
  130. }
  131.  
  132. return ans;
  133. }
  134.  
  135.  
  136.  
  137.  
  138. //* Maintaining Trick
  139.  
  140. int consistency4(int n){
  141.  
  142. unordered_map<int,int> mp;
  143.  
  144. int s = 0, e = 0;
  145.  
  146. int ans = 0;
  147.  
  148. while(e<n){
  149.  
  150. if(mp.count(a[e])){
  151. int j = mp[a[e]];
  152.  
  153. while(s<=j){
  154. mp.erase(a[s]);
  155. s++;
  156. }
  157. }
  158. mp[a[e]] = e;
  159. ans = max(ans, e-s+1);
  160. e++;
  161. }
  162.  
  163. return ans;
  164. }
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185. int practice(int n){
  186.  
  187.  
  188. return 0;
  189. }
  190.  
  191.  
  192.  
  193.  
  194.  
  195. void solve() {
  196.  
  197. int n;
  198. cin>> n;
  199.  
  200. a.resize(n);
  201. for(int i=0; i<n; i++) cin >> a[i];
  202.  
  203. cout << consistency1(n) << " " << consistency2(n) << " " << consistency3(n) << " " << consistency4(n) << endl;
  204.  
  205.  
  206. }
  207.  
  208.  
  209.  
  210.  
  211.  
  212. int32_t main() {
  213. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  214.  
  215. int t = 1;
  216. // cin >> t;
  217. while (t--) {
  218. solve();
  219. }
  220.  
  221. return 0;
  222. }
Success #stdin #stdout 0s 5312KB
stdin
10
3 2 4 5 2 6 7 8 9 10
stdout
8 8 8 8