fork(1) download
  1. process.stdin.resume();
  2. process.stdin.setEncoding('utf8');
  3.  
  4. // your code goes here
  5. const ordered = (a, b) => a < b;
  6.  
  7. function almostIncreasingSequence(xs) {
  8. for(var i=0, removed = false; i<xs.length - 1; ++i) {
  9. if(!ordered(xs[i], xs[i+1])) {
  10.  
  11. // если раньше убирали, а теперь непоследовательность второй раз
  12. // или уберём сейчас левый или правый, а последовательности не будет
  13. if(removed ||
  14. i > 0 && !ordered(xs[i-1], xs[i+1]) &&
  15. i + 2 < xs.length && !ordered(xs[i], xs[i+2])) return false;
  16.  
  17. // если выкинуть один, xs[0]..xs[i+1] - упорядочена
  18. removed = true;
  19. }
  20. }
  21.  
  22. return true;
  23. }
  24.  
  25. const least = -Infinity;
  26.  
  27. const isIncreasingSequence1 = (_, n, xs) => xs.reduce((r, x, i) =>
  28. i == n ? r : [r[0] && ordered(r[1], x), x], [true, least])[0];
  29.  
  30. const almostIncreasingSequenceNaive = xs =>
  31. isIncreasingSequence1(null,-1, xs) || xs.some(isIncreasingSequence1);
  32.  
  33. function seqInc(xs) {
  34. const dmin = -3, dmax = 3;
  35. for(var i=0; i<xs.length; ++i) {
  36. if(++xs[i] > dmax) xs[i] = dmin;
  37. else break;
  38. }
  39. if(i >= xs.length) {
  40. for(var i=0; i<xs.length; ++i) xs[i] = dmin;
  41. xs.push(dmin);
  42. }
  43. }
  44.  
  45. var seq = [];
  46.  
  47. while(seq.length < 7) {
  48. var a = almostIncreasingSequence(seq);
  49. var an = almostIncreasingSequenceNaive(seq);
  50. if(a != an) console.error(seq, a, an);
  51. seqInc(seq);
  52. }
Success #stdin #stdout 0.32s 29148KB
stdin
Standard input is empty
stdout
Standard output is empty