-
Notifications
You must be signed in to change notification settings - Fork 607
Added internal solution for Atcoder - Grouping #5937
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
Merged
Merged
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
11a9163
Change solutionMetadata kind to internal
Kiruthees d5947ca
Added internal solution for AC-Grouping as it did not have editorial …
Kiruthees f6adfcf
Merge branch 'cpinitiative:master' into Cses2421
Kiruthees ef46aeb
Revise ac-grouping.mdx with new structure and content
Kiruthees 7b6a081
Fix formatting and improve clarity in ac-grouping.mdx
Kiruthees e75fe60
Fix formatting and clarify explanation in ac-grouping.mdx
Kiruthees 4f9ea1f
minor fixes
Kiruthees e896c67
Merge branch 'master' into Cses2421
Kiruthees 2f6f4ef
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d80062b
Revise explanation and implementation for AC grouping
Kiruthees e3bd21e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 19232a7
Update solutions/gold/ac-grouping.mdx
Kiruthees 89164d6
resolved conversation
Kiruthees 38a06af
Merge branch 'master' into Cses2421
Kiruthees a8320a4
Update solutions/gold/ac-grouping.mdx
Kiruthees ef2f53e
committed formatting changes based on conversation
Kiruthees 492e0dc
made necessary changes as per eysbutno
Kiruthees c5ad0a6
Merge branch 'master' into Cses2421
Kiruthees b29b808
Merge branch 'master' into Cses2421
Kiruthees 22d5667
Merge branch 'master' into Cses2421
Kiruthees 7f01107
added explanation for why xor works here
Kiruthees 828d43b
Update ac-grouping.mdx
eysbutno 37c5582
Merge branch 'master' into Cses2421
eysbutno File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| --- | ||
| id: ac-grouping | ||
| source: AtCoder | ||
| title: Grouping | ||
| author: Kiruthees G | ||
| --- | ||
|
|
||
| ## Explanation | ||
|
|
||
| TThe goal is to partition a set of $N$ rabbits into any number of groups to maximize the total compatibility score. Since $N$ is small (up to 16), we use Bitmask DP. | ||
|
|
||
| ### 1. Bitmask Representation | ||
Kiruthees marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| An integer $i$ between $0$ and $2^N - 1$ represents a subset of rabbits. If the $j$-th bit is set ($1$), rabbit $j$ is included in the subset. | ||
|
|
||
| ### 2. DP State and Transition | ||
|
|
||
| We define `dp[i]` as the maximum score possible for the subset of rabbits represented by mask $i$. | ||
Kiruthees marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| The transition involves two steps for each mask $i$: | ||
|
|
||
| 1. **Base Score**: Initialize `dp[i]` by calculating the sum of $a_{j,k}$ for all pairs $(j, k)$ within the mask. This assumes all rabbits in the mask form a single group. | ||
| 2. **Merging Subsets**: Improve `dp[i]` by splitting the mask into two disjoint submasks $j$ and $i \char`\^ j$. The transition is: | ||
|
|
||
| $$dp[i] = \max_{j \subseteq i} \{ dp[j] + dp[i \char`\^ j] \}$$ | ||
Kiruthees marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| For an efficient way to iterate over submasks, we use the pattern `for (int j = i; j; j = (j - 1) & i)`. You can read more about this technique in the [USACO Guide: Merging Subsets](https://usaco.guide/gold/dp-bitmasks?lang=cpp#merging-subsets). | ||
|
|
||
| ### Additional Resources | ||
|
|
||
| * [USACO Guide: Bitmask DP](https://usaco.guide/gold/dp-bitmasks?lang=cpp) | ||
| * [Unofficial Editorial (Nwatx)](https://nwatx.me/post/atcoderdp) | ||
Kiruthees marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| --- | ||
Kiruthees marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Implementation | ||
|
|
||
| **Time Complexity**: $$\mathcal{O}(3^N + N^2 \cdot 2^N)$$ | ||
Kiruthees marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
Kiruthees marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ```cpp | ||
| #include <bits/stdc++.h> | ||
| using namespace std; | ||
|
|
||
| using ll = long long; | ||
|
|
||
| int main() { | ||
| ios_base::sync_with_stdio(false); | ||
| cin.tie(nullptr); | ||
|
|
||
| int n; | ||
| cin >> n; | ||
| vector a(n, vector<int>(n)); | ||
| for (int i = 0; i < n; i++) { | ||
| for (int j = 0; j < n; j++) { cin >> a[i][j]; } | ||
| } | ||
|
|
||
| vector<ll> dp(1 << n); | ||
| // Iterate through every possible subset of rabbits | ||
| for (int i = 1; i < (1 << n); i++) { | ||
| // Calculate internal score if this subset forms ONE single group | ||
| for (int j = 0; j < n; j++) { | ||
| if (!(i >> j & 1)) continue; | ||
| for (int k = j + 1; k < n; k++) { | ||
| if (!(i >> k & 1)) continue; | ||
| dp[i] += a[j][k]; | ||
| } | ||
| } | ||
|
|
||
| // Try splitting the mask into smaller groups to find a better score | ||
| for (int j = (i - 1) & i; j > 0; j = (j - 1) & i) { | ||
Kiruthees marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| dp[i] = max(dp[i], dp[j] + dp[i ^ j]); | ||
| } | ||
| } | ||
|
|
||
| cout << dp.back() << '\n'; | ||
Kiruthees marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.