結果

問題 No.723 2つの数の和
ユーザー jp_stejp_ste
提出日時 2020-05-07 20:52:32
言語 JavaScript
(node v20.8.0)
結果
AC  
実行時間 269 ms / 2,000 ms
コード長 3,274 bytes
コンパイル時間 75 ms
コンパイル使用メモリ 5,332 KB
実行使用メモリ 56,940 KB
最終ジャッジ日時 2023-08-03 03:57:11
合計ジャッジ時間 5,338 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
42,420 KB
testcase_01 AC 72 ms
42,380 KB
testcase_02 AC 70 ms
42,192 KB
testcase_03 AC 216 ms
50,232 KB
testcase_04 AC 264 ms
51,796 KB
testcase_05 AC 241 ms
51,100 KB
testcase_06 AC 246 ms
51,396 KB
testcase_07 AC 155 ms
48,840 KB
testcase_08 AC 164 ms
49,088 KB
testcase_09 AC 262 ms
52,752 KB
testcase_10 AC 153 ms
48,548 KB
testcase_11 AC 91 ms
44,668 KB
testcase_12 AC 177 ms
49,072 KB
testcase_13 AC 269 ms
53,120 KB
testcase_14 AC 128 ms
48,256 KB
testcase_15 AC 159 ms
48,860 KB
testcase_16 AC 192 ms
49,804 KB
testcase_17 AC 233 ms
50,840 KB
testcase_18 AC 177 ms
48,740 KB
testcase_19 AC 206 ms
56,940 KB
testcase_20 AC 68 ms
42,240 KB
testcase_21 AC 69 ms
42,296 KB
testcase_22 AC 69 ms
42,332 KB
testcase_23 AC 213 ms
51,260 KB
testcase_24 AC 137 ms
47,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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"));
0