結果
| 問題 |
No.723 2つの数の和
|
| コンテスト | |
| ユーザー |
jp_ste
|
| 提出日時 | 2020-05-07 20:52:32 |
| 言語 | JavaScript (node v23.5.0) |
| 結果 |
AC
|
| 実行時間 | 278 ms / 2,000 ms |
| コード長 | 3,274 bytes |
| コンパイル時間 | 54 ms |
| コンパイル使用メモリ | 6,692 KB |
| 実行使用メモリ | 53,708 KB |
| 最終ジャッジ日時 | 2024-10-13 01:48:23 |
| 合計ジャッジ時間 | 4,917 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 22 |
ソースコード
function main(input) {
let [N, K] = getIntegers(input, " ");
let A = getIntegers(input, " ");
A.sortByIntegers();
let count = 0;
for(let i=0; i<N; i++) {
let V = K - A[i];
let num = upper(A, V) - lower(A, V);
count += num;
}
console.log(count);
}
//-- functions ------------------------------------
function lower(L, K, l=0, r=L.length) {
l--;
while(r - l > 1) {
let x = Math.floor((r + l) / 2);
if(L[x] >= K) {
r = x;
} else {
l = x;
}
}
return r;
}
function upper(L, K, l=0, r=L.length) {
l--;
while(r - l > 1) {
let x = Math.floor((r + l) / 2);
if(L[x] > K) {
r = x;
} else {
l = x;
}
}
return r;
}
function getIntegers(lines, sep) {
if(sep === undefined) {
return lines.map(function(e) {
return Number(e);
});
} else {
return lines.shift().split(sep).map(function(e) {
if(e == '+' || e == '*') return e;
return Number(e);
});
}
}
function getBigIntegers(lines, sep=" ") {
return lines.shift().split(sep).map(function(e) {
return BigInt(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);
}
const lower_bound = (arr, val, first = 0, last = arr.length) => {
first -= 1; // 一番端の位置からさらにずらす
while (last - first > 1) {
const mid = first + Math.floor((last - first) / 2);
if (arr[mid] < val)
first = mid;
else
last = mid;
}
return last;
}
const upper_bound = (arr, val, first = 0, last = arr.length) => {
first -= 1;
while (last - first > 1) {
const mid = first - Math.floor((last - first) / 2);
if (arr[mid] <= val) // ココを直すだけ
first = mid;
else
last = mid;
}
return last;
}
//-- Array -------------------------------------------
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);
};
Array.prototype.sortByIntegers = function() {
this.sort(function(a, b) {
return a - b;
});
};
main(require("fs").readFileSync("/dev/stdin", "utf8").trim().split("\n"));
jp_ste