-
Notifications
You must be signed in to change notification settings - Fork 540
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
Adding casework sub-section under "Ad Hoc" #5199
Open
enplasmatic
wants to merge
13
commits into
cpinitiative:master
Choose a base branch
from
enplasmatic:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+191
−18
Open
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2614ae7
Update Ad_Hoc.mdx [added casework]
enplasmatic 74f63e6
Update Ad_Hoc.problems.json [added casework problems]
enplasmatic a6a0cdc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 83150fa
Added
enplasmatic c221d51
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 23eb6bd
Cleaned up c++ code for sleepy cow sol
enplasmatic 8652ba7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] edbee02
fixed indent in python sol
enplasmatic d8e5b17
Update Ad_Hoc.problems.json [fixed consistency issue]
enplasmatic 5489d1a
formatting
Sosuke23 9dfb4ba
Merge branch 'master' into master
enplasmatic aeba897
fixed indent in python sol [again]
enplasmatic 953e031
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
id: ad-hoc | ||
title: Ad Hoc Problems | ||
author: Michael Cao | ||
author: Michael Cao, Aarav Sharma | ||
contributors: Ryan Chou | ||
description: | ||
'Problems that do not fall into standard categories with well-studied | ||
|
@@ -322,14 +322,154 @@ for i in range(n): | |
</PySection> | ||
</LanguageSection> | ||
## Problems | ||
# Casework | ||
When you draw a lot of cases and notice that certain cases fit similar trends in their solutions, chances are that the problem requires **casework**. A casework problem is a type of ad hoc problem that needs to be broken down into different cases that each need to be accounted for. | ||
Casework problems also require drawing out a lot of cases and making observations about these cases. Try to spot similarities and differences between cases and their solutions. | ||
## Example - Sleepy Cow Herding | ||
<FocusProblem problem="case-tutorial" /> | ||
<Spoiler title="Hint 1"> | ||
The solution for the minimum involves casework, while the maximum does not necessarily. | ||
</Spoiler> | ||
<Spoiler title="Hint 2"> | ||
There are 3 cases for the minimum, each having their own $\mathcal{O}(1)$ solution. Can you find out what those solutions are? | ||
</Spoiler> | ||
There are 3 cases for the minimum amount of moves: | ||
- The 3 positions are already consecutive. | ||
- Two elements are already in-position consecutively (including gaps), but the other is not. | ||
- Any other case that did not satisfy the two above. | ||
For the first case, the answer would be $0$ because the elements are already consecutive. | ||
For the second case, the answer would be $1$ because the only swap required is the one that would insert the isolated element into the gap between the two other elements. | ||
The third case would output $2$ because for any other test case, the optimal solution would be to take the minimum element to $max-2$, and then the new minimum to fit right in between the gap. | ||
The maximum will always be finite because these operations group the cows closer together over time, as mentioned in the problem statement. The best approach to maximize the amount of moves is to place each element as close to a gap as possible (while not remaining an endpoint). Therefore, the maximum is just the largest gap between two adjacent elements minus 1. This can be observed by drawing a lot of cases. | ||
This code was borrowed from [here.](https://usaco.guide/problems/usaco-915-sleepy-cow-herding/solution) | ||
<LanguageSection> | ||
<CPPSection> | ||
```cpp | ||
#include <algorithm> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
int main() { | ||
freopen("herding.in", "r", stdin); | ||
freopen("herding.out", "w", stdout); | ||
|
||
// all cow locations | ||
vector<int> a; | ||
for (int i = 0; i < 3; i++) { | ||
int b; | ||
cin >> b; | ||
a.push_back(b); | ||
} | ||
sort(a.begin(), a.end()); | ||
|
||
/* | ||
* The minimum number of moves can only be 0, 1, or 2. | ||
* 0 is if they're already consecutive, | ||
* 1 is if there's a difference of 2 between any 2 numbers, | ||
* and 2 is for all other cases. | ||
*/ | ||
if (a[0] == a[2] - 2) { | ||
cout << 0 << endl; | ||
} else if ((a[1] == a[2] - 2) || (a[0] == a[1] - 2)) { | ||
cout << 1 << endl; | ||
} else { | ||
cout << 2 << endl; | ||
} | ||
// max is equal to largest difference between end and middle, minus one. | ||
cout << max(a[2] - a[1], a[1] - a[0]) - 1; | ||
} | ||
``` | ||
</CPPSection> | ||
<JavaSection> | ||
```java | ||
import java.io.*; | ||
import java.util.*; | ||
class Main { | ||
public static void main(String[] args) throws IOException { | ||
Scanner sc = new Scanner(new File("herding.in")); | ||
PrintWriter pw = new PrintWriter(new File("herding.out")); | ||
int[] cows = new int[3]; | ||
cows[0] = sc.nextInt(); | ||
cows[1] = sc.nextInt(); | ||
cows[2] = sc.nextInt(); | ||
Arrays.sort(cows); | ||
|
||
// Printing the minimum number of moves | ||
if (cows[2] == cows[0] + 2) { | ||
pw.println(0); | ||
} else if (cows[1] == cows[0] + 2 || cows[2] == cows[1] + 2) { | ||
pw.println(1); | ||
} else { | ||
pw.println(2); | ||
} | ||
|
||
// Max number of moves | ||
pw.println(Math.max(cows[1] - cows[0], cows[2] - cows[1]) - 1); | ||
pw.close(); | ||
} | ||
} | ||
``` | ||
</JavaSection> | ||
<PySection> | ||
```py | ||
import sys | ||
|
||
sys.stdin = open("herding.in", "r") | ||
sys.stdout = open("herding.out", "w") | ||
enplasmatic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
a, b, c = map(int, input().split()) | ||
|
||
# Best scenario: the three elements are already in order. | ||
if c == a + 2: | ||
print(0) | ||
""" | ||
If there is a difference by 2, it can be solved in one move. | ||
3 5 9 -> 5 7 9 | ||
""" | ||
elif b == a + 2 or c == b + 2: | ||
print(1) | ||
""" | ||
It can always be solved in two moves by moving a -> c - 2 and b -> c - 1. | ||
If there is less than one integer between the two elements, it'll be taken care | ||
of in the if statement above. | ||
""" | ||
else: | ||
print(2) | ||
""" | ||
The worst case is incrementing by 1 in the largest gap. | ||
3 5 9 -> 5 6 9 -> 6 7 9 -> 7 8 9 | ||
""" | ||
print(max(b - a, c - b) - 1) | ||
``` | ||
</PySection> | ||
</LanguageSection> | ||
## Ad Hoc Problems | ||
Of course, ad hoc problems can be | ||
[quite easy](/general/expected-knowledge#problem-usaco-591), but the ones | ||
presented below are generally on the harder side. | ||
<Problems problems="general" /> | ||
## Casework Problems | ||
<Problems problems="casework" /> | ||
## Quiz | ||
<Quiz> | ||
|
@@ -354,4 +494,26 @@ presented below are generally on the harder side. | |
</Quiz.Explanation> | ||
</Quiz.Answer> | ||
</Quiz.Question> | ||
<Quiz.Question> | ||
What is casework? | ||
<Quiz.Answer correct> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not necessary ngl There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
Breaking down a problem into general cases that follow trends in their solutions. | ||
<Quiz.Explanation> | ||
Correct. | ||
</Quiz.Explanation> | ||
</Quiz.Answer> | ||
<Quiz.Answer> | ||
The act of drawing out a lot of cases. | ||
<Quiz.Explanation> | ||
Incorrect. While drawing out a lot of cases is crucial in solving a casework problem, that is not the definition of casework. | ||
</Quiz.Explanation> | ||
</Quiz.Answer> | ||
<Quiz.Answer> | ||
A technique used to optimize searching. | ||
<Quiz.Explanation> | ||
Incorrect. | ||
</Quiz.Explanation> | ||
</Quiz.Answer> | ||
</Quiz.Question> | ||
</Quiz> |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not make a vector of size 3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i mean we do end up making one anyway in the next lines
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is just unnecessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i got the c++ code from the usaco.guide solution from the problem, so thats why its like that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i changed it tho