255D - Mr Bender and Square - CodeForces Solution


binary search implementation math *1800

Please click on ads to support us..

C++ Code:

#include <algorithm>
#include <iostream>

using namespace std;

long long count(int i, int j, int m) {
long long k = (long long) m * (m + 1) / 2;
if (m > i)
k -= (long long) (m - i) * (m - i + 1) / 2;
if (m > j)
k -= (long long) (m - j) * (m - j + 1) / 2;
if (m > i + j)
k += (long long) (m - i - j) * (m - i - j + 1) / 2;
return k;
}

int main() {
int n, i, j; long long k_; cin >> n >> i >> j >> k_;
int lower = -1, upper = n * 2;
while (upper - lower > 1) {
int m = (lower + upper) / 2;
if (count(i - 1, j - 1, m - 1) + count(i - 1, n - j, m - 1) + count(n - i, j - 1, m - 1) + count(n - i, n - j, m - 1) + min(m, i - 1) + min(m, j - 1) + min(m, n - i) + min(m, n - j) + 1 >= k_)
upper = m;
else
lower = m;
}
cout << upper << '\n';
return 0;
}


Comments

Submit
0 Comments
More Questions

1720A - Burenka Plays with Fractions
3A - Shortest path of the king
1720C - Corners
574A - Bear and Elections
352B - Jeff and Periods
1244A - Pens and Pencils
1670A - Prof Slim
1189A - Keanu Reeves
678A - Johny Likes Numbers
1699C - The Third Problem
1697D - Guess The String
754B - Ilya and tic-tac-toe game
760A - Petr and a calendar
1573A - Countdown
166A - Rank List
1631B - Fun with Even Subarrays
727A - Transformation from A to B
822B - Crossword solving
1623A - Robot Cleaner
884B - Japanese Crosswords Strike Back
862B - Mahmoud and Ehab and the bipartiteness
429A - Xor-tree
1675C - Detective Task
950A - Left-handers Right-handers and Ambidexters
672B - Different is Good
1C - Ancient Berland Circus
721A - One-dimensional Japanese Crossword
1715B - Beautiful Array
60B - Serial Time
453A - Little Pony and Expected Maximum