fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5.  
  6.  
  7. const int M = 1000000007;
  8. const int N = 3e5+9;
  9. const int INF = 2e9+1;
  10. const int MAXN = 100000;
  11. const int LINF = 2000000000000000001;
  12.  
  13. //_ ***************************** START Below *******************************
  14.  
  15.  
  16.  
  17. vector<int> a;
  18.  
  19.  
  20. //* Template1 for Atleast k (i.e. >= k )
  21.  
  22.  
  23. void consistency1(int n, int k){
  24.  
  25. int maxi = 0;
  26. for(int i=0; i<n; i++) maxi = max(maxi, a[i]);
  27.  
  28. int s = 0, e = 0;
  29. int ct = 0;
  30. int ans = 0;
  31.  
  32. while(e<n){
  33. //* Calculate State
  34. if(a[e] == maxi) ct++;
  35.  
  36. //* Invalid window => keep expanding
  37. if(ct < k){
  38. e++;
  39. }
  40. else{
  41. //* Valid window => keep shrinking && Computing Result
  42. while(s<=e && ct >= k){
  43. ans += n-e;
  44. if(a[s] == maxi) ct--;
  45. s++;
  46. }
  47. e++;
  48. }
  49. }
  50. cout << ans << endl;
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. void consistency2(int n, int k){
  59.  
  60. int maxi = 0;
  61. for(int i=0; i<n; i++) maxi = max(maxi, a[i]);
  62.  
  63. int s = 0, e = 0;
  64. int ct = 0;
  65. int ans = 0;
  66.  
  67. while(e<n){
  68. //* Calculate State
  69. if(a[e] == maxi) ct++;
  70.  
  71. //* Valid window => keep shrinking && Computing Result
  72. while(s<=e && ct >= k){
  73. ans += n-e;
  74. if(a[s] == maxi) ct--;
  75. s++;
  76. }
  77. e++;
  78. }
  79. cout << ans << endl;
  80. }
  81.  
  82.  
  83.  
  84. void solve() {
  85.  
  86. int n, k;
  87. cin >> n >> k;
  88. a.resize(n);
  89. for(int i=0; i<n; i++) cin >> a[i];
  90.  
  91. consistency1(n, k);
  92. consistency2(n, k);
  93.  
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100. int32_t main() {
  101. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  102.  
  103. int t = 1;
  104. while (t--) {
  105. solve();
  106. }
  107.  
  108. return 0;
  109. }
Success #stdin #stdout 0.01s 5264KB
stdin
5 2
1 3 2 3 3
stdout
6
6