結果

問題 No.927 Second Permutation
ユーザー jp_stejp_ste
提出日時 2020-05-10 21:48:02
言語 JavaScript
(node v21.7.1)
結果
WA  
実行時間 -
コード長 2,918 bytes
コンパイル時間 87 ms
コンパイル使用メモリ 5,376 KB
実行使用メモリ 46,720 KB
最終ジャッジ日時 2024-04-21 03:30:22
合計ジャッジ時間 3,834 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
39,168 KB
testcase_01 AC 62 ms
39,168 KB
testcase_02 AC 58 ms
39,296 KB
testcase_03 AC 60 ms
39,424 KB
testcase_04 AC 58 ms
39,168 KB
testcase_05 AC 62 ms
39,424 KB
testcase_06 AC 64 ms
39,168 KB
testcase_07 AC 61 ms
39,296 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 77 ms
44,928 KB
testcase_25 AC 76 ms
44,416 KB
testcase_26 AC 72 ms
43,776 KB
testcase_27 AC 69 ms
43,904 KB
testcase_28 AC 75 ms
44,928 KB
testcase_29 AC 78 ms
44,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

function main(input) {
  let X = getStrings(input, "");
  X.sortByIntegerD();
  let XX = X.join("");
  let ans = "";
  if(X.length == 2) {
    ans = X[1] + X[0];
  } else {
    for(let i=2; i<X.length; i++) {
      if(X[i-1] != X[i]) {
        let n = X[i-1];
        X[i-1] = X[i];
        X[i] = n;
        ans = X.join("");
        break;
      }
    }
  }
  if(ans == "" || ans[0] == '0' || ans == XX) {
    console.log(-1);
  } else {
    console.log(ans);
  }
}

//-- functions ------------------------------------

function getIntegers(lines, sep) {
  if(sep === undefined) {
    return lines.map(function(e) {
      return Number(e);
    });  
  } else {
    return lines.shift().split(sep).map(function(e) {
      return Number(e);
    });  
  }
}

function getStrings(lines, sep=" ") {
  return lines.shift().split(sep);
}

function twoDimenArrayByValue(h, w, value) {
  const list = new Array(h);
  for(let i=0; i<h; i++) {
    list[i] = new Array(w).fill().map(function(e) {
      return value.constructor == Array ? Array.from(value) : value;
    });
  }
  return list;
}

function twoDimenArrayByStrings(h, strings) {
  const list = new Array(h);
  for(let i=0; i<h; i++) {
    list[i] = strings.shift().split("");
  }
  return list;
}

function distance(x1, y1, x2, y2) {
  return Math.sqrt((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1));
}

function gcd(a, b) {
  return b === 0 ? a : gcd(b, a % b);
}

//-- Array -------------------------------------------

Array.prototype.sortByIntegerA = function() {
  this.sort(function(a, b) { return a - b; });
};

Array.prototype.sortByIntegerD = function() {
  this.sort(function(a, b) { return b - a; });
};

Array.prototype.orLess = function(V, l=0, r=this.length) {
  l--;
  while(r - l > 1) {
    let x = Math.floor((r + l) / 2);
    this[x] > V ? r = x : l = x;
  }
  return r;
};

Array.prototype.lessThen = function(V, l=0, r=this.length) {
  l--;
  while(r - l > 1) {
    let x = Math.floor((r + l) / 2);
    this[x] >= V ? r = x : l = x;
  }
  return r;
};

Array.prototype.orHigher = function(V, l, r) {
  return this.length - this.lessThen(V, l, r);
};

Array.prototype.greaterThan = function(V, l, r) {
  return this.length - this.orLess(V, l, r);
};

Array.prototype.deepCopy = function(list) {
  let retList = [];
  for(let i=0; i<list.length; i++) {
    retList[i] = list[i].constructor == Array ? Array.from(list[i]) : list[i];
  }
  return retList;
};

Array.prototype.out = function() {
  let outList = [];
  if(this[0].constructor == Array) {
    for(let i=0; i<this.length; i++) {
      outList.push(this[i].join(" "));
    } 
  } else {
    outList.push(this.join(" "));
  }
  console.log(outList.join("\n"));
};

Array.prototype.pushNoSameValue = function(...values) {
  values.forEach(function(e) {
    if(!this.includes(e)) {
      this.push(e);
    }
  }, this);
};

main(require("fs").readFileSync("/dev/stdin", "utf8").trim().split("\n"));
0