fork download
  1. "use strict";
  2.  
  3. const round = [];
  4. let run = 0;
  5. let max = 0;
  6. let first_arr = [];
  7. let last_arr = [];
  8. let dishes;
  9. let kind;
  10. let eat;
  11. let coupon;
  12. let use_coupon = false;
  13. let inputs = require("fs").readFileSync("/dev/stdin", "utf8").trim().split("\n");
  14.  
  15. let first_line = inputs[0].split(' ').map(Number);
  16. dishes = parseInt(first_line[0]);
  17. kind = parseInt(first_line[1]);
  18. eat = parseInt(first_line[2]);
  19. coupon = parseInt(first_line[3]);
  20. inputs.shift();
  21.  
  22. for (let run = 0; run < inputs.length; run++) {
  23. round.push(inputs[run]);
  24. if (run == eat) first_arr = round.slice();
  25. else if (run == dishes - eat + 2) last_arr = round;
  26.  
  27. if (round.length == eat + 2) {
  28. use_coupon = false;
  29. setMaxCount(round);
  30. round.shift();
  31. }
  32. }
  33.  
  34. const arr = last_arr.concat(first_arr);
  35. arr.pop();
  36. let t = [];
  37. let temp;
  38.  
  39. for (let i = 0; i < last_arr.length + eat - 1; i++) {
  40. t = [];
  41. for (let j = i; j < i + eat + 2; j++) {
  42. if (j > arr.length - 1) temp = j - arr.length;
  43. else temp = j;
  44. t.push(arr[temp]);
  45. }
  46. setMaxCount(t);
  47. }
  48. console.log(max);
  49.  
  50. function setMaxCount(arr) {
  51. let use_coupon = false;
  52.  
  53. let temp_max = arr.filter((element, index) => {
  54. if (index > 0 && index < arr.length) {
  55. if (element == coupon) use_coupon = true;
  56. return arr.indexOf(element) === index;
  57. } else return false;
  58. }).length;
  59.  
  60. if (!use_coupon && (arr[0] == coupon || arr[arr.length - 1] == coupon)) temp_max++;
  61. if (max < temp_max) max = temp_max;
  62.  
  63. return max;
  64. }
Success #stdin #stdout 0.06s 32028KB
stdin
Standard input is empty
stdout
0