fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn=1e7+14;
  4. int n, p, q, chan[maxn], le[maxn];
  5. void solve(){
  6. cin >> n >> p >> q;
  7. memset(chan, 0, sizeof chan);
  8. memset(le, 0, sizeof le);
  9. for (int i = 1; i <= n; i++){
  10. int x;
  11. cin >> x;
  12. if (x % 2) le[i] = 1;
  13. else chan[i] = 1;
  14. }
  15.  
  16. if ( !p && !q) {
  17. cout << 1 ;
  18. return;
  19. }
  20.  
  21. int cntchan = 0, cntle = 0;
  22. for (int i = 1; i <= n; i++) {
  23. cntchan += chan[i];
  24. cntle += le[i];
  25. }
  26. if (cntchan < p || cntle < q) {
  27. cout << -1;
  28. return;
  29. }
  30.  
  31. int ans = n;
  32. int l = 1, r = 1, cntp = 0, cntq = 0;
  33. while (r <= n) {
  34. while ((cntp < p || cntq < q) && r <= n) {
  35. cntp += chan[r];
  36. cntq += le[r];
  37. r++;
  38. }
  39.  
  40. while (cntp >= p && cntq >= q && l <= r) {
  41. cntp -= chan[l];
  42. cntq -= le[l];
  43. l++;
  44. }
  45. l --;
  46. cntp += chan[l];
  47. cntq += le[l];
  48. if (cntp >= p && cntq >= q) {
  49. ans = min(ans, r - l);
  50. }
  51.  
  52. cntp -= chan[l];
  53. cntq -= le[l];
  54. l++;
  55. }
  56.  
  57. cout << ans ;
  58. }
  59. int main(){
  60. ios_base::sync_with_stdio(0);
  61. cin.tie(0); cout.tie(0);
  62. if (fopen("input.txt","r")){
  63. freopen("input.txt","r",stdin);
  64. freopen("output.txt","w",stdout);
  65. }
  66. solve();
  67. return 0;
  68. }
  69.  
Success #stdin #stdout 0.02s 81752KB
stdin
0 0 0
stdout
1