add: 220518 + CN [cpp]

This commit is contained in:
Eat-Swap 2022-05-18 23:04:39 +08:00
parent e9d90c396d
commit 4943988171
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
3 changed files with 50 additions and 1 deletions

42
cpp/2205/220518-CN.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <cstdio>
/**
* 668. Kth Smallest Number in Multiplication Table
* Nearly everyone has used the Multiplication Table. The multiplication table of size m x n is an integer matrix mat where mat[i][j] == i * j (1-indexed).
* Given three integers m, n, and k, return the kth smallest element in the m x n multiplication table.
*/
class Solution {
public:
static constexpr int findKthNumber(int m, int n, int k) {
auto compute = [&](int upper_bound) {
int ans = 0;
for (int i = 1; i <= m; ++i)
ans += (upper_bound / i <= n) ? (upper_bound / i) : n;
return ans;
};
int L = 1, R = m * n, M;
// [L, R]
while (L < R) {
M = (L + R) >> 1;
int x = compute(M);
if (x > k) {
R = M;
} else if (x < k) {
L = M + 1;
} else {
break;
}
}
M = (L + R + 1) >> 1;
for (int c = compute(M), p; (p = compute(M - 1)) >= k || p == c; --M);
return M;
}
};
int main() {
std::printf("%d\n", Solution::findKthNumber(2, 3 ,6));
return 0;
}

View File

@ -1,6 +1,13 @@
#include <vector> #include <vector>
#include <functional> #include <functional>
/**
* 1192. Critical Connections in a Network
* There are n servers numbered from 0 to n - 1 connected by undirected server-to-server connections forming a network where connections[i] = [ai, bi] represents a connection between servers ai and bi. Any server can reach other servers directly or indirectly through the network.
* A critical connection is a connection that, if removed, will make some servers unable to reach some other server.
* Return all critical connections in the network in any order.
*/
class Solution { class Solution {
public: public:
static std::vector<std::vector<int>> criticalConnections(int n, const std::vector<std::vector<int>>& e) { static std::vector<std::vector<int>> criticalConnections(int n, const std::vector<std::vector<int>>& e) {

View File

@ -3,4 +3,4 @@ PROJECT(2205)
SET(CMAKE_CXX_STANDARD 23) SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2205 220518.cpp) ADD_EXECUTABLE(2205 220518-CN.cpp)