Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Level Generation Editorial #4966

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions content/3_Silver/Binary_Search.problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@
"isStarred": false,
"tags": ["Binary Search"],
"solutionMetadata": {
"kind": "autogen-label-from-site",
"site": "CF"
"kind": "internal",
"hasHints": true
}
},
{
Expand Down
88 changes: 88 additions & 0 deletions solutions/silver/cf-818F.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
id: cf-818F
source: CF
title: Level Generation
author: Justin Ji
---

<Spoiler title="Hint 1">

What is the upper bound for the number of bridges we can have?

</Spoiler>

<Spoiler title="Answer to Hint 1">

The optimal construction would be to create a tree, which has $n - 1$ bridges.
TheGamingMousse marked this conversation as resolved.
Show resolved Hide resolved

</Spoiler>

<Spoiler title="Hint 2">

Let's consider allocating a certain amount of these nodes to forming a tree.
How can we use the rest of these nodes to use as many edges as possible?

</Spoiler>

<Spoiler title="Explanation">

The most bridges we can have in a graph with $n$ nodes is $n - 1$ bridges. For
those interested, the reason this is the case is that all of our bridges must be part of an
arbitrary spanning tree of our graph.
TheGamingMousse marked this conversation as resolved.
Show resolved Hide resolved

As a result, our answer is in the range $[n - 1, 2n - 2]$. Let's consider binary
searching on our answer. If we have $x$ edges that we need to use, then
$\lfloor \frac{x + 1}{2} \rfloor$ of these edges must be a bridge.

Recall that the best way to create bridges is to create a tree. Thus, we use all of these
bridge edges to form a tree, and then use the one extra edge to connect this tree
to some component of nodes. With the rest of our nodes, we can form a complete
graph of nodes to use as many edges as possible.

## Implementation

**Time Complexity:** $\mathcal{O}(Q\log{N})$

<LanguageSection>
<CPPSection>

```cpp
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
int test_num;
cin >> test_num;
for (int i = 0; i < test_num; i++) {
TheGamingMousse marked this conversation as resolved.
Show resolved Hide resolved
int nodes;
cin >> nodes;

/** @return if we can make a graph with this many edges */
const auto check = [&](ll edges) -> bool {
TheGamingMousse marked this conversation as resolved.
Show resolved Hide resolved
int num_bridges = (edges + 1) / 2;
int cycle_nodes = nodes - num_bridges;
ll cycle_edges = 1ll * cycle_nodes * (cycle_nodes - 1) / 2;
return edges - num_bridges <= cycle_edges;
};

ll low = nodes - 1;
ll high = 2ll * (nodes - 1);
while (low < high) {
ll mid = (low + high + 1) / 2;
if (check(mid)) {
low = mid;
} else {
high = mid - 1;
}
}
cout << low << '\n';
}
}
```

</CPPSection>
</LanguageSection>

</Spoiler>
Loading