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.  
  33. //* Template 1 :
  34.  
  35. int consistency1(int n, int m, int k){
  36.  
  37. unordered_map<int,int> big, sm;
  38.  
  39. int s = 0, e = 0;
  40. int l = 0;
  41.  
  42. int ans = 0;
  43.  
  44. int distinct = 0; //* total distinct ele in big window
  45. int count = 0; //* total >=m ele's in small window
  46.  
  47. while(e<n){
  48. big[a[e]]++;
  49. if(big[a[e]] == 1) distinct++;
  50.  
  51. sm[a[e]]++;
  52. if(sm[a[e]] == m) count++;
  53.  
  54. while(l<=e && count >= k){
  55. sm[a[l]]--;
  56. if(sm[a[l]] == m-1) count--;
  57. if(sm[a[l]] == 0) sm.erase(a[l]);
  58. l++;
  59. }
  60.  
  61. if(distinct < k){
  62. e++;
  63. }
  64. else{
  65. while(s<=e && distinct > k){
  66.  
  67. big[a[s]]--;
  68. if(big[a[s]] == 0){
  69. distinct--;
  70. big.erase(a[s]);
  71. }
  72. s++;
  73. }
  74.  
  75. if(l>=s) ans += l-s;
  76.  
  77. e++;
  78. }
  79. }
  80.  
  81. return ans;
  82. }
  83.  
  84.  
  85.  
  86.  
  87.  
  88. //* Template 2 :
  89.  
  90. int consistency2(int n, int m, int k){
  91.  
  92. unordered_map<int,int> big, sm;
  93.  
  94. int s = 0, e = 0;
  95. int l = 0;
  96.  
  97. int ans = 0;
  98.  
  99. int distinct = 0; //* total distinct ele in big window
  100. int count = 0; //* total >=m ele's in small window
  101.  
  102. while(e<n){
  103. big[a[e]]++;
  104. if(big[a[e]] == 1) distinct++;
  105.  
  106. sm[a[e]]++;
  107. if(sm[a[e]] == m) count++;
  108.  
  109. while(l<=e && count >= k){
  110. sm[a[l]]--;
  111. if(sm[a[l]] == m-1) count--;
  112. if(sm[a[l]] == 0) sm.erase(a[l]);
  113. l++;
  114. }
  115.  
  116. while(s<=e && distinct > k){
  117.  
  118. big[a[s]]--;
  119. if(big[a[s]] == 0){
  120. distinct--;
  121. big.erase(a[s]);
  122. }
  123. s++;
  124. }
  125.  
  126. if(l>=s) ans += l-s;
  127.  
  128. e++;
  129. }
  130.  
  131. return ans;
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153. int practice(int n, int m, int k){
  154.  
  155.  
  156. return 0;
  157. }
  158.  
  159.  
  160.  
  161.  
  162.  
  163. void solve() {
  164.  
  165. int n, m, k;
  166. cin>> n >> m >> k;
  167.  
  168. a.resize(n);
  169. for(int i=0; i<n; i++) cin >> a[i];
  170.  
  171. cout << consistency1(n, m, k) << " " << consistency2(n, m, k) << endl;
  172.  
  173.  
  174. }
  175.  
  176.  
  177.  
  178.  
  179.  
  180. int32_t main() {
  181. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  182.  
  183. int t = 1;
  184. // cin >> t;
  185. while (t--) {
  186. solve();
  187. }
  188.  
  189. return 0;
  190. }
Success #stdin #stdout 0s 5320KB
stdin
9 2 2
2 2 2 1 1 2 2 3 3
stdout
10 10