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. inline int power(int a, int b) {
  7. int x = 1;
  8. while (b) {
  9. if (b & 1) x *= a;
  10. a *= a;
  11. b >>= 1;
  12. }
  13. return x;
  14. }
  15.  
  16.  
  17. const int M = 1000000007;
  18. const int N = 3e5+9;
  19. const int INF = 2e9+1;
  20. const int LINF = 2000000000000000001;
  21.  
  22. //_ ***************************** START Below *******************************
  23.  
  24. vector<int> a;
  25.  
  26. int consistency(int n, int k) {
  27.  
  28. vector<int> f(32, 0);
  29. int oor = 0;
  30. int ans = n+1;
  31. int s = 0, e = 0;
  32. while(e<n){
  33. for(int i=0; i<32; i++){
  34. if(a[e]&(1<<i)) f[i]++;
  35. }
  36. oor |= a[e];
  37. if(oor < k){
  38. e++;
  39. }
  40. else{
  41. while(s<=e && oor >= k){
  42. ans = min(ans, e-s+1);
  43. int newOr = 0;
  44. for(int i=0; i<32; i++){
  45. if(a[s]&(1<<i)) f[i]--;
  46. if(f[i] > 0) newOr = (newOr | (1<<i));
  47. }
  48. oor = newOr;
  49. s++;
  50. }
  51. e++;
  52. }
  53. }
  54.  
  55. if(ans == n+1) return -1;
  56.  
  57. return ans;
  58. }
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. int practice(int n, int k) {
  69.  
  70.  
  71. return 0;
  72. }
  73.  
  74.  
  75. void solve() {
  76.  
  77. int n, k;
  78. cin>>n >> k;
  79.  
  80. a.resize(n);
  81. for(int i=0; i<n; i++) cin >> a[i];
  82.  
  83. cout << consistency(n, k) << endl;
  84. // cout << consistency(n, k) << " -> " << practice(n, k) << endl;
  85.  
  86.  
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93. int32_t main() {
  94. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  95.  
  96. int t = 1;
  97. // cin >> t;
  98. while (t--) {
  99. solve();
  100. }
  101.  
  102. return 0;
  103. }
Success #stdin #stdout 0.01s 5280KB
stdin
3 2
1 3 2
stdout
1