From e1fcff9f8aa3d4b48c2a2706b5d58d4e44602f3c Mon Sep 17 00:00:00 2001 From: David Guo Date: Mon, 13 Jan 2025 16:39:50 -0800 Subject: [PATCH 01/18] Update usaco-714.mdx --- solutions/silver/usaco-714.mdx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/solutions/silver/usaco-714.mdx b/solutions/silver/usaco-714.mdx index 20216a02b0..44a452147e 100644 --- a/solutions/silver/usaco-714.mdx +++ b/solutions/silver/usaco-714.mdx @@ -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 From b0fef691743745f9747f35a8bfc8af78d228c76c Mon Sep 17 00:00:00 2001 From: freakin23 Date: Tue, 14 Jan 2025 21:33:28 +0530 Subject: [PATCH 02/18] update problems.json --- content/3_Silver/More_Prefix_Sums.problems.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/3_Silver/More_Prefix_Sums.problems.json b/content/3_Silver/More_Prefix_Sums.problems.json index 23028b010c..14f9fa1b40 100644 --- a/content/3_Silver/More_Prefix_Sums.problems.json +++ b/content/3_Silver/More_Prefix_Sums.problems.json @@ -68,7 +68,7 @@ "isStarred": false, "tags": ["Difference Array"], "solutionMetadata": { - "kind": "none" + "kind": "internal" } } ], From e81e57665f09786d216cd6c545b120d3fbf0fba8 Mon Sep 17 00:00:00 2001 From: freakin23 Date: Tue, 14 Jan 2025 21:35:35 +0530 Subject: [PATCH 03/18] add layout --- solutions/silver/cf-276C.mdx | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 solutions/silver/cf-276C.mdx diff --git a/solutions/silver/cf-276C.mdx b/solutions/silver/cf-276C.mdx new file mode 100644 index 0000000000..748428b886 --- /dev/null +++ b/solutions/silver/cf-276C.mdx @@ -0,0 +1,23 @@ +--- +id: cf-276C +source: CF +title: Little Girl and Maximum Sum +author: Rameez Parwez +--- + +## Explanation + + +## Implementation + +**Time Complexity:** $$ + + + + +```cpp + +``` + + + \ No newline at end of file From 5df43cd72d882815a3fb4c0c83b74e9a1e7569dd Mon Sep 17 00:00:00 2001 From: freakin23 Date: Tue, 14 Jan 2025 21:54:15 +0530 Subject: [PATCH 04/18] update cf-276C --- solutions/silver/cf-276C.mdx | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/solutions/silver/cf-276C.mdx b/solutions/silver/cf-276C.mdx index 748428b886..ab2c8e9f82 100644 --- a/solutions/silver/cf-276C.mdx +++ b/solutions/silver/cf-276C.mdx @@ -7,6 +7,8 @@ author: Rameez Parwez ## Explanation +Calculate the involvement 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 @@ -16,7 +18,39 @@ author: Rameez Parwez ```cpp - +#include + +int main() { + int n, q; + std::cin >> n >> q; + std::vector arr(n); + + for (auto &x : arr) { + std::cin >> x; + } + + std::vector freq(n + 1); + while (q--) { + 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(rbegin(freq), rend(freq)); + std::sort(rbegin(arr), rend(arr)); + long long res = 0; + + for (int i = 0; i < n; i++) { + res += 1LL * freq[i] * arr[i]; + } + std::cout << res << '\n'; +} ``` From bae3b3e4177ed672bd6dea70723bc60aa1bba804 Mon Sep 17 00:00:00 2001 From: freakin23 Date: Tue, 14 Jan 2025 22:00:15 +0530 Subject: [PATCH 05/18] add py sol --- solutions/silver/cf-276C.mdx | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/solutions/silver/cf-276C.mdx b/solutions/silver/cf-276C.mdx index ab2c8e9f82..946aae7dfb 100644 --- a/solutions/silver/cf-276C.mdx +++ b/solutions/silver/cf-276C.mdx @@ -12,7 +12,7 @@ To maximize the sum, sort both the array and frequency array in descending order ## Implementation -**Time Complexity:** $$ +**Time Complexity:** $ \mathcal{O} (N \log N)$ @@ -54,4 +54,30 @@ int main() { ``` + + +```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) +``` + + \ No newline at end of file From e92c3373595e0603a3553ec39d361e15855e7bfc Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 14 Jan 2025 16:33:39 +0000 Subject: [PATCH 06/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/silver/cf-276C.mdx | 68 ++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 37 deletions(-) diff --git a/solutions/silver/cf-276C.mdx b/solutions/silver/cf-276C.mdx index 946aae7dfb..32f307ebdf 100644 --- a/solutions/silver/cf-276C.mdx +++ b/solutions/silver/cf-276C.mdx @@ -21,35 +21,29 @@ To maximize the sum, sort both the array and frequency array in descending order #include int main() { - int n, q; - std::cin >> n >> q; - std::vector arr(n); - - for (auto &x : arr) { - std::cin >> x; - } - - std::vector freq(n + 1); - while (q--) { - 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(rbegin(freq), rend(freq)); - std::sort(rbegin(arr), rend(arr)); - long long res = 0; - - for (int i = 0; i < n; i++) { - res += 1LL * freq[i] * arr[i]; - } - std::cout << res << '\n'; + int n, q; + std::cin >> n >> q; + std::vector arr(n); + + for (auto &x : arr) { std::cin >> x; } + + std::vector freq(n + 1); + while (q--) { + 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(rbegin(freq), rend(freq)); + std::sort(rbegin(arr), rend(arr)); + long long res = 0; + + for (int i = 0; i < n; i++) { res += 1LL * freq[i] * arr[i]; } + std::cout << res << '\n'; } ``` @@ -62,22 +56,22 @@ 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 + 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[i] += freq[i - 1] -freq.sort(reverse = True) -arr.sort(reverse = True) +freq.sort(reverse=True) +arr.sort(reverse=True) res = 0 for i in range(n): - res += freq[i] * arr[i] + res += freq[i] * arr[i] print(res) ``` - \ No newline at end of file + From 51ff3d44cdeb219c2cfb7f745119c6a92a4dd5dc Mon Sep 17 00:00:00 2001 From: Kevin Sheng Date: Tue, 14 Jan 2025 08:45:05 -0800 Subject: [PATCH 07/18] use archived mark nelson links --- content/2_Bronze/Complete_Rec.mdx | 2 +- content/4_Gold/Hashmaps.mdx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/content/2_Bronze/Complete_Rec.mdx b/content/2_Bronze/Complete_Rec.mdx index 1d606f5ed9..a5fb52328c 100644 --- a/content/2_Bronze/Complete_Rec.mdx +++ b/content/2_Bronze/Complete_Rec.mdx @@ -494,7 +494,7 @@ for perm in perms: explanation with an example diff --git a/content/4_Gold/Hashmaps.mdx b/content/4_Gold/Hashmaps.mdx index 48de2a2db5..5832aacb46 100644 --- a/content/4_Gold/Hashmaps.mdx +++ b/content/4_Gold/Hashmaps.mdx @@ -49,7 +49,7 @@ in the code above) or declare our own hash function. How to create user-defined hash function for `unordered_map`. From 6e046c95306414641becf8d211bdcdb8a34ed716 Mon Sep 17 00:00:00 2001 From: Kevin Sheng Date: Tue, 14 Jan 2025 08:46:21 -0800 Subject: [PATCH 08/18] fix prolem name --- content/4_Gold/LIS.problems.json | 2 +- solutions/gold/cfgym-102951C.mdx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/content/4_Gold/LIS.problems.json b/content/4_Gold/LIS.problems.json index a00ec91bcd..78e400c4ff 100644 --- a/content/4_Gold/LIS.problems.json +++ b/content/4_Gold/LIS.problems.json @@ -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", diff --git a/solutions/gold/cfgym-102951C.mdx b/solutions/gold/cfgym-102951C.mdx index 5bf5271040..edca1ceb22 100644 --- a/solutions/gold/cfgym-102951C.mdx +++ b/solutions/gold/cfgym-102951C.mdx @@ -1,7 +1,7 @@ --- id: cfgym-102951C source: CF -title: LIS on Permutations +title: LCS on Permutations author: Dong Liu --- From 453bcb59ae2fc01ffa3425995bfab094fab802c9 Mon Sep 17 00:00:00 2001 From: Kevin Sheng <55369003+SansPapyrus683@users.noreply.github.com> Date: Tue, 14 Jan 2025 08:52:15 -0800 Subject: [PATCH 09/18] Update cf-276C.mdx --- solutions/silver/cf-276C.mdx | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/solutions/silver/cf-276C.mdx b/solutions/silver/cf-276C.mdx index 32f307ebdf..07cd4db250 100644 --- a/solutions/silver/cf-276C.mdx +++ b/solutions/silver/cf-276C.mdx @@ -8,7 +8,8 @@ author: Rameez Parwez ## Explanation Calculate the involvement 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. +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 @@ -25,10 +26,10 @@ int main() { std::cin >> n >> q; std::vector arr(n); - for (auto &x : arr) { std::cin >> x; } + for (int &x : arr) { std::cin >> x; } std::vector freq(n + 1); - while (q--) { + for (int i = 0; i < q; i++) { int l, r; std::cin >> l >> r; l--; @@ -40,10 +41,10 @@ int main() { std::sort(rbegin(freq), rend(freq)); std::sort(rbegin(arr), rend(arr)); - long long res = 0; + long long res = 0; for (int i = 0; i < n; i++) { res += 1LL * freq[i] * arr[i]; } - std::cout << res << '\n'; + std::cout << res << endl; } ``` @@ -65,8 +66,8 @@ for i in range(1, n + 1): freq.sort(reverse=True) arr.sort(reverse=True) -res = 0 +res = 0 for i in range(n): res += freq[i] * arr[i] From b81c88f05aaf5fd2da2d69e40a6b353b46ce437c Mon Sep 17 00:00:00 2001 From: Rameez Parwez <79394137+Sosuke23@users.noreply.github.com> Date: Tue, 14 Jan 2025 22:30:49 +0530 Subject: [PATCH 10/18] Update cf-276C.mdx --- solutions/silver/cf-276C.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solutions/silver/cf-276C.mdx b/solutions/silver/cf-276C.mdx index 07cd4db250..0b60e02d6b 100644 --- a/solutions/silver/cf-276C.mdx +++ b/solutions/silver/cf-276C.mdx @@ -39,8 +39,8 @@ int main() { for (int i = 1; i <= n; i++) { freq[i] += freq[i - 1]; } - std::sort(rbegin(freq), rend(freq)); - std::sort(rbegin(arr), rend(arr)); + sort(rbegin(freq), rend(freq)); + sort(rbegin(arr), rend(arr)); long long res = 0; for (int i = 0; i < n; i++) { res += 1LL * freq[i] * arr[i]; } From e4105191f5f5321bb59c18f58d4a360498558c5d Mon Sep 17 00:00:00 2001 From: Rameez Parwez <79394137+Sosuke23@users.noreply.github.com> Date: Wed, 15 Jan 2025 10:29:02 +0530 Subject: [PATCH 11/18] Update solutions/silver/cf-276C.mdx Co-authored-by: envyaims <79723780+envyaims@users.noreply.github.com> --- solutions/silver/cf-276C.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/silver/cf-276C.mdx b/solutions/silver/cf-276C.mdx index 0b60e02d6b..007f0ed706 100644 --- a/solutions/silver/cf-276C.mdx +++ b/solutions/silver/cf-276C.mdx @@ -7,7 +7,7 @@ author: Rameez Parwez ## Explanation -Calculate the involvement of each index in the range query using a difference array technique. +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. From d55b42140567705c1273d7279bd4c8fc30bc07f3 Mon Sep 17 00:00:00 2001 From: freakin23 Date: Wed, 15 Jan 2025 13:45:50 +0530 Subject: [PATCH 12/18] update problems.json --- content/3_Silver/Sorting_Custom.problems.json | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/content/3_Silver/Sorting_Custom.problems.json b/content/3_Silver/Sorting_Custom.problems.json index 44804b20a5..19ce162d2f 100644 --- a/content/3_Silver/Sorting_Custom.problems.json +++ b/content/3_Silver/Sorting_Custom.problems.json @@ -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", From 0be00edbcc8d294212f244b8719dd5704824cc0b Mon Sep 17 00:00:00 2001 From: Rameez Parwez <79394137+Sosuke23@users.noreply.github.com> Date: Wed, 15 Jan 2025 21:35:25 +0530 Subject: [PATCH 13/18] Update cf-276C.mdx --- solutions/silver/cf-276C.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solutions/silver/cf-276C.mdx b/solutions/silver/cf-276C.mdx index 007f0ed706..2e9f33112f 100644 --- a/solutions/silver/cf-276C.mdx +++ b/solutions/silver/cf-276C.mdx @@ -39,8 +39,8 @@ int main() { for (int i = 1; i <= n; i++) { freq[i] += freq[i - 1]; } - sort(rbegin(freq), rend(freq)); - sort(rbegin(arr), rend(arr)); + std::sort(rbegin(freq), rend(freq)); + std::sort(rbegin(arr), rend(arr)); long long res = 0; for (int i = 0; i < n; i++) { res += 1LL * freq[i] * arr[i]; } From 008f1a2ef9bf0db96501e348d9641ef206ddcf27 Mon Sep 17 00:00:00 2001 From: Rameez Parwez <79394137+Sosuke23@users.noreply.github.com> Date: Wed, 15 Jan 2025 21:44:42 +0530 Subject: [PATCH 14/18] Update cf-276C.mdx --- solutions/silver/cf-276C.mdx | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/solutions/silver/cf-276C.mdx b/solutions/silver/cf-276C.mdx index 2e9f33112f..3b8e3cf8ee 100644 --- a/solutions/silver/cf-276C.mdx +++ b/solutions/silver/cf-276C.mdx @@ -19,7 +19,12 @@ and pair the elements of the array with their respective frequencies. ```cpp -#include +#include +#include +#include + +using std::rbegin; +using std::rend; int main() { int n, q; @@ -39,8 +44,8 @@ int main() { for (int i = 1; i <= n; i++) { freq[i] += freq[i - 1]; } - std::sort(rbegin(freq), rend(freq)); - std::sort(rbegin(arr), rend(arr)); + sort(rbegin(freq), rend(freq)); + sort(rbegin(arr), rend(arr)); long long res = 0; for (int i = 0; i < n; i++) { res += 1LL * freq[i] * arr[i]; } From e5955f50dd8a25088f81ab1f4a23df7edab74a00 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 15 Jan 2025 16:15:54 +0000 Subject: [PATCH 15/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/silver/cf-276C.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/silver/cf-276C.mdx b/solutions/silver/cf-276C.mdx index 3b8e3cf8ee..bdb9ebaeff 100644 --- a/solutions/silver/cf-276C.mdx +++ b/solutions/silver/cf-276C.mdx @@ -19,9 +19,9 @@ and pair the elements of the array with their respective frequencies. ```cpp +#include #include #include -#include using std::rbegin; using std::rend; From 793e17ceaca24b8c8e3e8b0437fbe56aedd10e5c Mon Sep 17 00:00:00 2001 From: Kevin Sheng <55369003+SansPapyrus683@users.noreply.github.com> Date: Wed, 15 Jan 2025 08:17:55 -0800 Subject: [PATCH 16/18] Update cf-276C.mdx --- solutions/silver/cf-276C.mdx | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/solutions/silver/cf-276C.mdx b/solutions/silver/cf-276C.mdx index bdb9ebaeff..f4b33c8b24 100644 --- a/solutions/silver/cf-276C.mdx +++ b/solutions/silver/cf-276C.mdx @@ -8,6 +8,7 @@ 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. @@ -23,9 +24,6 @@ and pair the elements of the array with their respective frequencies. #include #include -using std::rbegin; -using std::rend; - int main() { int n, q; std::cin >> n >> q; @@ -44,8 +42,8 @@ int main() { for (int i = 1; i <= n; i++) { freq[i] += freq[i - 1]; } - sort(rbegin(freq), rend(freq)); - sort(rbegin(arr), rend(arr)); + 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]; } From b35e9082010ab18c5dee1203443af2a8401d2636 Mon Sep 17 00:00:00 2001 From: Kevin Sheng <55369003+SansPapyrus683@users.noreply.github.com> Date: Wed, 15 Jan 2025 08:19:06 -0800 Subject: [PATCH 17/18] Update cf-276C.mdx --- solutions/silver/cf-276C.mdx | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/solutions/silver/cf-276C.mdx b/solutions/silver/cf-276C.mdx index f4b33c8b24..26692b7799 100644 --- a/solutions/silver/cf-276C.mdx +++ b/solutions/silver/cf-276C.mdx @@ -24,14 +24,18 @@ and pair the elements of the array with their respective frequencies. #include #include +using std::cout; +using std::endl; +using std::vector; + int main() { int n, q; std::cin >> n >> q; - std::vector arr(n); + vector arr(n); for (int &x : arr) { std::cin >> x; } - std::vector freq(n + 1); + vector freq(n + 1); for (int i = 0; i < q; i++) { int l, r; std::cin >> l >> r; @@ -47,7 +51,8 @@ int main() { long long res = 0; for (int i = 0; i < n; i++) { res += 1LL * freq[i] * arr[i]; } - std::cout << res << endl; + + cout << res << endl; } ``` From 31e584bdd629e19f7e3276616e73231a365e5a66 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 15 Jan 2025 16:20:27 +0000 Subject: [PATCH 18/18] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/silver/cf-276C.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/solutions/silver/cf-276C.mdx b/solutions/silver/cf-276C.mdx index 26692b7799..4d6d88dcd4 100644 --- a/solutions/silver/cf-276C.mdx +++ b/solutions/silver/cf-276C.mdx @@ -51,7 +51,7 @@ int main() { long long res = 0; for (int i = 0; i < n; i++) { res += 1LL * freq[i] * arr[i]; } - + cout << res << endl; } ```