-
Notifications
You must be signed in to change notification settings - Fork 586
Do You Know Your Abcs Silver #5787
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 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
19f694d
Create documentation for USACO 1135 problem
sachet-abey 3c6ba1f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6086aa1
Update problem name and solution metadata
sachet-abey 16b685b
Merge branch 'master' into sachet-abey-patch-1
guodavid0418 48005b1
Add official analysis link to usaco-1135.mdx
sachet-abey a016a89
Update usaco-1135.mdx
sachet-abey 92e7a35
change TC to O(T) since n is so small
guodavid0418 95cc0b6
Merge branch 'master' into sachet-abey-patch-1
guodavid0418 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
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,111 @@ | ||
| --- | ||
| id: usaco-1135 | ||
| source: USACO Silver 2021 US Open | ||
| title: Do You Know Your ABCs? | ||
| author: Sachet Abeysinghe | ||
| --- | ||
|
|
||
| ## Explanation | ||
|
|
||
| Due to the low bounds, we can use a brute-force approach. There are seven labels $A$, $B$, $C$, $A + B$, $B + C$, $A + C$, and $A + B + C$. We need exactly $N$ of them, so we can iterate over all combinations of $N$ labels to decide which labels appear in this test case. | ||
|
|
||
| For a chosen set of labels, we need to decide which number corresponds to which label. We iterate over all permutations of the chosen labels, mapping them to the input array in order. Finally, we need to deduce the unique triple $(A, B, C)$ consistent with the mappings. To do this, we track candidate values for $A$, $B$, and $C$ using sets. If each of $A$, $B$, and $C$ has one consistent value, we record the triple as valid. | ||
eysbutno marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Implementation | ||
|
|
||
| **Time Complexity:** $\mathcal{O}(T\cdot\frac{7!}{\left(7-N\right)!})$ | ||
|
|
||
| <LanguageSection> | ||
|
|
||
| <PySection> | ||
|
|
||
| ```py | ||
| import itertools | ||
|
|
||
| for i in range(int(input())): | ||
| N = int(input()) | ||
| arr = [int(x) for x in input().split()] | ||
|
|
||
| def deduce_ABC(tags): | ||
| a = b = c = ab = bc = ac = abc = -1 | ||
| for i in range(N): | ||
| value = arr[i] | ||
| tag = tags[i] | ||
| if tag == "A": | ||
| a = value | ||
| if tag == "B": | ||
| b = value | ||
| if tag == "C": | ||
| c = value | ||
| if tag == "A+B": | ||
| ab = value | ||
| if tag == "B+C": | ||
| bc = value | ||
| if tag == "A+C": | ||
| ac = value | ||
| if tag == "A+B+C": | ||
| abc = value | ||
|
|
||
| possible_a, possible_b, possible_c = set(), set(), set() | ||
| if a != -1: | ||
| possible_a.add(a) | ||
| if ab != -1: | ||
| possible_b.add(ab - a) | ||
| if ac != -1: | ||
| possible_c.add(ac - a) | ||
| if b != -1: | ||
| possible_b.add(b) | ||
| if ab != -1: | ||
| possible_a.add(ab - b) | ||
| if bc != -1: | ||
| possible_c.add(bc - b) | ||
| if c != -1: | ||
| possible_c.add(c) | ||
| if ac != -1: | ||
| possible_a.add(ac - c) | ||
| if bc != -1: | ||
| possible_b.add(bc - c) | ||
| if abc != -1: | ||
| if bc != -1: | ||
| possible_a.add(abc - bc) | ||
| if ac != -1: | ||
| possible_b.add(abc - ac) | ||
| if ab != -1: | ||
| possible_c.add(abc - ab) | ||
|
|
||
| if len(possible_a) != 1 or len(possible_b) != 1 or len(possible_c) != 1: | ||
| return | ||
|
|
||
| final_a, final_b, final_c = ( | ||
| list(possible_a)[0], | ||
| list(possible_b)[0], | ||
| list(possible_c)[0], | ||
| ) | ||
| if not 1 <= final_a <= final_b <= final_c: | ||
| return | ||
| if ( | ||
| (ac != -1 and final_a + final_c != ac) | ||
| or (ab != -1 and final_a + final_b != ab) | ||
| or (bc != -1 and final_b + final_c != bc) | ||
| or (abc != -1 and final_a + final_b + final_c != abc) | ||
| ): | ||
| return | ||
| return (final_a, final_b, final_c) | ||
|
|
||
| valid_triples = set() | ||
| combinations = itertools.combinations( | ||
| ["A", "B", "C", "A+B", "B+C", "A+C", "A+B+C"], N | ||
| ) | ||
| for combination in combinations: | ||
| permutations = itertools.permutations(combination) | ||
| for permutation in permutations: | ||
| triple = deduce_ABC(permutation) | ||
| if triple != None: | ||
| valid_triples.add(triple) | ||
|
|
||
| print(len(valid_triples)) | ||
| ``` | ||
|
|
||
| </PySection> | ||
|
|
||
| </LanguageSection> | ||
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.