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

update Watching Mooloo #4893

Merged
merged 7 commits into from
Nov 5, 2024
Merged
Changes from 5 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
48 changes: 34 additions & 14 deletions solutions/bronze/usaco-1301.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ author: Chu Minjae

## Explanation

We solve this problem with a greedy algorithm by first sorting all the days Bessie wants to watch Mooloo.
We solve this problem with a greedy algorithm.

Next, we iterate through all the days.
First, we iterate through all the days.
On the first day, Bessie will always have to buy a new subscription.
However, on subsequent days, there's two cases:
1. It's better to extend the last subscription.
Expand All @@ -38,21 +38,19 @@ int main() {
vector<long long> days(n);
for (long long &d : days) { cin >> d; }

sort(days.begin(), days.end());

long long last_day = days[0];
long long cost = k + 1; // Start the first subscription
for (long long d : days) {
for (int i = 1; i < n; i++) {
// Should Bessie extend the most recent subscription?
if (d - last_day < k + 1) {
cost += d - last_day;
if (days[i] - last_day < k + 1) {
cost += days[i] - last_day;
SansPapyrus683 marked this conversation as resolved.
Show resolved Hide resolved
} else {
// Or just start a new one entirely?
cost += k + 1;
}

// Store the date of the last subscription
last_day = d;
last_day = days[i];
}

cout << cost << endl;
Expand All @@ -78,21 +76,19 @@ public class WatchingMooloo {
br.close();
for (int i = 0; i < n; i++) { days[i] = Long.parseLong(daysInput[i]); }

Arrays.sort(days);
SansPapyrus683 marked this conversation as resolved.
Show resolved Hide resolved

long lastDay = days[0];
long cost = k + 1; // Start the first subscription
for (long d : days) {
for (int i = 1; i < n; i++) {
SansPapyrus683 marked this conversation as resolved.
Show resolved Hide resolved
// Should Bessie extend the most recent subscription?
if (d - lastDay < k + 1) {
cost += d - lastDay;
if (days[i] - lastDay < k + 1) {
cost += days[i] - lastDay;
} else {
// Or just start a new one entirely?
cost += k + 1;
}

// Store the date of the last subscription
lastDay = d;
lastDay = days[i];
}

System.out.println(cost);
Expand All @@ -101,4 +97,28 @@ public class WatchingMooloo {
```

</JavaSection>
<PySection>

```py
n, k = map(int, input().split())
days = list(map(int, input().split()))

last_day = days[0]
cost = k + 1 # Start the first subscription

for i in range(1, n):
# Should Bessie extend the most recent subscription?
if days[i] - last_day < k + 1:
cost += days[i] - last_day
else:
# Or just start a new one entirely?
cost += k + 1

# Store the date of the last subscription
last_day = days[i]

print(cost)
```

</PySection>
</LanguageSection>
Loading