Skip to content

Commit

Permalink
Merge branch 'master' into patch-7
Browse files Browse the repository at this point in the history
  • Loading branch information
JoltedCowIceCream authored Jan 15, 2025
2 parents 855da01 + 0be11df commit 94b4a97
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 23 deletions.
2 changes: 1 addition & 1 deletion content/2_Bronze/Complete_Rec.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ for perm in perms:
<Resource
source="Mark Nelson"
title="Next Permutation"
url="https://marknelson.us/posts/2002/03/01/next-permutation.html"
url="https://web.archive.org/web/20240327002825/https://marknelson.us/posts/2002/03/01/next-permutation.html"
starred
>
explanation with an example
Expand Down
2 changes: 1 addition & 1 deletion content/3_Silver/More_Prefix_Sums.problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"isStarred": false,
"tags": ["Difference Array"],
"solutionMetadata": {
"kind": "none"
"kind": "internal"
}
}
],
Expand Down
14 changes: 0 additions & 14 deletions content/3_Silver/Sorting_Custom.problems.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
{
"MODULE_ID": "sorting-custom",
"sample": [
{
"uniqueId": "usaco-992",
"name": "Wormhole Sort",
"url": "http://www.usaco.org/index.php?page=viewproblem2&cpid=992",
"source": "Silver",
"difficulty": "Insane",
"isStarred": false,
"tags": [],
"solutionMetadata": {
"kind": "internal"
}
}
],
"sample2": [
{
"uniqueId": "usaco-1063",
Expand Down
2 changes: 1 addition & 1 deletion content/4_Gold/Hashmaps.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ in the code above) or declare our own hash function.
<Resource
source="Mark Nelson"
title="Hash Functions for C++ Unordered Containers"
url="https://marknelson.us/posts/2011/09/03/hash-functions-for-c-unordered-containers.html"
url="https://web.archive.org/web/20240524031508/https://marknelson.us/posts/2011/09/03/hash-functions-for-c-unordered-containers.html"
starred
>
How to create user-defined hash function for `unordered_map`.
Expand Down
2 changes: 1 addition & 1 deletion content/4_Gold/LIS.problems.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
},
{
"uniqueId": "cfgym-102951C",
"name": "LIS on Permutations",
"name": "LCS on Permutations",
"url": "https://codeforces.com/gym/102951/problem/C",
"source": "CF",
"difficulty": "Normal",
Expand Down
2 changes: 1 addition & 1 deletion solutions/gold/cfgym-102951C.mdx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
id: cfgym-102951C
source: CF
title: LIS on Permutations
title: LCS on Permutations
author: Dong Liu
---

Expand Down
86 changes: 86 additions & 0 deletions solutions/silver/cf-276C.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
---
id: cf-276C
source: CF
title: Little Girl and Maximum Sum
author: Rameez Parwez
---

## Explanation

We can calculate the contribution of each index in the range query using a difference array technique.

To maximize the sum, sort both the array and frequency array in descending order
and pair the elements of the array with their respective frequencies.

## Implementation

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

<LanguageSection>
<CPPSection>

```cpp
#include <algorithm>
#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

int main() {
int n, q;
std::cin >> n >> q;
vector<int> arr(n);

for (int &x : arr) { std::cin >> x; }

vector<int> freq(n + 1);
for (int i = 0; i < q; i++) {
int l, r;
std::cin >> l >> r;
l--;
freq[l] += 1;
freq[r] -= 1;
}

for (int i = 1; i <= n; i++) { freq[i] += freq[i - 1]; }

std::sort(std::rbegin(freq), std::rend(freq));
std::sort(std::rbegin(arr), std::rend(arr));

long long res = 0;
for (int i = 0; i < n; i++) { res += 1LL * freq[i] * arr[i]; }

cout << res << endl;
}
```

</CPPSection>
<PySection>

```py
n, q = map(int, input().split())
arr = list(map(int, input().split()))

freq = [0] * (n + 1)
for _ in range(q):
l, r = map(int, input().split())
freq[l - 1] += 1
freq[r] -= 1

for i in range(1, n + 1):
freq[i] += freq[i - 1]

freq.sort(reverse=True)
arr.sort(reverse=True)

res = 0
for i in range(n):
res += freq[i] * arr[i]

print(res)
```

</PySection>
</LanguageSection>
8 changes: 4 additions & 4 deletions solutions/silver/usaco-714.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
id: usaco-714
source: USACO Silver 2017 February
title: Why Did the Cow Cross the Road I
author: Benjamin Qi, Brad Ma
author: Benjamin Qi, Brad Ma, David Guo
---

[Official Analysis (C++)](http://www.usaco.org/current/data/sol_helpcross_silver_feb17.html)

There are multiple greedy strategies (though all of them involve
sorting the animals by time). Here, we follow the approach described by the
analysis.
## Explanation

Let’s focus on the chicken with the earliest time preference for helping a cow. If no cow needs to cross the road at that time, we can disregard this chicken. However, if there are cows that need to cross at that time, we have some options for assigning the chicken. Intuitively, we should assign this chicken to the cow whose crossing time window ends the earliest. This approach maximizes flexibility for assigning other chickens to cows later.

## Implementation

Expand Down

0 comments on commit 94b4a97

Please sign in to comment.