Skip to content

Instantly share code, notes, and snippets.

@RomanSteinberg
Created July 13, 2018 08:11
Show Gist options
  • Save RomanSteinberg/8df8c6962a0dde3154de65a9af000c51 to your computer and use it in GitHub Desktop.
Save RomanSteinberg/8df8c6962a0dde3154de65a9af000c51 to your computer and use it in GitHub Desktop.
solution
function fk(n) {
return (n*(n + 1)) / 2;
}
var data = readline().split(' ').map(function(x) { return parseInt(x); });
var n = data[0];
var m = data[1];
var k = data[2];
var nL = k - 1;
var nR = n - k;
var max = 1;
var i = 1;
var j = m;
while(i < j) {
var kk = Math.ceil((j + i) / 2);
if(kk == max) {
break;
}
var sR = 0;
var sL = 0;
if(nR > 0) {
if(kk < nR) {
sR = fk(kk - 1) + (nR - kk);
} else {
sR = fk(kk - 1) - fk(kk - 1 - nR);
}
}
if(nL > 0) {
if(kk < nL) {
sL = fk(kk - 1) + (nL - kk);
} else {
sL = fk(kk - 1) - fk(kk - 1 - nL);
}
}
var s = kk + sR + sL;
if(s <= m) {
max = kk;
i = kk;
} else {
j = kk;
}
}
print(max);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment