From 178308bbd5f678de78355bcba3e924035d9c3485 Mon Sep 17 00:00:00 2001 From: Benjamin Qi Date: Mon, 8 Jun 2020 21:41:23 -0400 Subject: [PATCH] misc --- content/2_General/3_WhyCpp.md | 59 +++++++++++++++++++++++++ content/2_General/7_Macros.md | 4 ++ content/4_Silver/0_Silver_Complexity.md | 32 +++++++------- content/4_Silver/5_Silver_2P.md | 6 +-- content/4_Silver/6_Silver_Psum.md | 2 +- 5 files changed, 84 insertions(+), 19 deletions(-) diff --git a/content/2_General/3_WhyCpp.md b/content/2_General/3_WhyCpp.md index 28e9341..a0b973f 100644 --- a/content/2_General/3_WhyCpp.md +++ b/content/2_General/3_WhyCpp.md @@ -168,6 +168,65 @@ Although both Python and Java receive two times the C++ time limit in USACO, thi +
+ Comparable C++ Solution (< 700ms) + + ```cpp + #include + using namespace std; + + typedef vector vi; + + const int MX = 1e5+5; + + int loc[MX], comp[MX], lhs[MX], rhs[MX], wei[MX]; + vi adj[MX]; + int n,m; + + void dfs(int cur, int label) { + if (comp[cur] == label) return; + comp[cur] = label; + for (int c: adj[cur]) dfs(c,label); + } + + bool valid(int minW) { + for (int i = 0; i < n; ++i) { + comp[i] = -1; + adj[i].clear(); + } + for (int i = 0; i < m; ++i) if (wei[i] >= minW) + adj[lhs[i]].push_back(rhs[i]), adj[rhs[i]].push_back(lhs[i]); + int numComps = 0; + for (int i = 0; i < n; ++i) if (comp[i] < 0) + dfs(i,numComps++); + for (int i = 0; i < n; ++i) + if (comp[i] != comp[loc[i]]) return 0; + return 1; + } + + int main() { + ios_base::sync_with_stdio(0); cin.tie(0); + freopen("wormsort.in","r",stdin); + freopen("wormsort.out","w",stdout); + cin >> n >> m; + for (int i = 0; i < n; ++i) cin >> loc[i], loc[i] --; + for (int i = 0; i < m; ++i) { + cin >> lhs[i], lhs[i] --; + cin >> rhs[i], rhs[i] --; + cin >> wei[i]; + } + int minW = 0, maxW = (int)1e9+1; + while (minW != maxW) { + int mid = (minW+maxW+1)/2; + if (valid(mid)) minW = mid; + else maxW = mid-1; + } + if (minW > 1e9) minW = -1; + cout << minW; + } + ``` +
+ ## Other - Python lacks a data structure that keeps its keys in sorted order (the equivalent of `set` in C++), which is required for some silver problems. diff --git a/content/2_General/7_Macros.md b/content/2_General/7_Macros.md index 11b3cb1..352192a 100644 --- a/content/2_General/7_Macros.md +++ b/content/2_General/7_Macros.md @@ -5,6 +5,10 @@ author: Benjamin Qi order: 7 --- +Shortening code and making it more (un?)readable. + + + ## Introduction - [GeeksForGeeks](https://www.geeksforgeeks.org/cc-preprocessors/#:~:text=Macros%3A%20Macros%20are%20a%20piece,used%20to%20define%20a%20macro.) diff --git a/content/4_Silver/0_Silver_Complexity.md b/content/4_Silver/0_Silver_Complexity.md index 8c6ae4b..eeaa657 100644 --- a/content/4_Silver/0_Silver_Complexity.md +++ b/content/4_Silver/0_Silver_Complexity.md @@ -1,7 +1,7 @@ --- slug: /silver/complexity -title: "Time & Space Complexity" -author: Darren Yao +title: "Time Complexity" +author: Darren Yao, Benjamin Qi order: 0 --- @@ -113,12 +113,13 @@ Complexity factors that come from some common algorithms and data structures are - Mathematical formulas that just calculate an answer: $O(1)$ - Unordered set/map: $O(1)$ per operation - Binary search: $O(\log n)$ -- Ordered set/map or priority queue: $O(log n)$ per operation +- Ordered set/map or priority queue: $O(\log n)$ per operation - Prime factorization of an integer, or checking primality or compositeness of an integer naively: $O(\sqrt{n})$ - Reading in $n$ items of input: $O(n)$ - Iterating through an array or a list of $n$ elements: $O(n)$ -- Sorting: usually $O(n \log n)$ for default sorting algorithms (mergesort, for example Collections.sort or Arrays.sort on objects) -- Java Quicksort Arrays.sort function on primitives on pathological worst-case data sets, don't use this in CodeForces rounds +- Sorting: usually $O(n \log n)$ for default sorting algorithms (mergesort, for example `Collections.sort` or `Arrays.sort` on objects) +- Java Quicksort `Arrays.sort` function on primitives: $O(n^2)$ + - on pathological worst-case data sets, don't use this in CodeForces rounds - Iterating through all subsets of size $k$ of the input elements: $O(n^k)$. For example, iterating through all triplets is $O(n^3)$. - Iterating through all subsets: $O(2^n)$ - Iterating through all permutations: $O(n!)$ @@ -126,16 +127,17 @@ Complexity factors that come from some common algorithms and data structures are Here are conservative upper bounds on the value of $n$ for each time complexity. You can probably get away with more than this, but this should allow you to quickly check whether an algorithm is viable. -- $n$ | Possible complexities -- $n \le 10$ | $O(n!)$, $O(n^7)$, $O(n^6)$ -- $n \le 20$ | $O(2^n \cdot n)$, $O(n^5)$ -- $n \le 80$ | $O(n^4)$ -- $n \le 400$ | $O(n^3)$ -- $n \le 7500$ | $O(n^2)$ -- $n \le 7 \cdot 10^4$ | $O(n \sqrt n)$ -- $n \le 5 \cdot 10^5$ | $O(n \log n)$ -- $n \le 5 \cdot 10^6$ | $O(n)$ -- $n \le 10^{18}$ | $O(\log^2 n)$, $O(\log n)$, $O(1)$ +| $n$ | Possible complexities | +| --------------------- | ----------------------------------- | +| $n \le 10$ | $O(n!)$, $O(n^7)$, $O(n^6)$ | +| $n \le 20$ | $O(2^n \cdot n)$, $O(n^5)$ | +| $n \le 80$ | $O(n^4)$ | +| $n \le 400$ | $O(n^3)$ | +| $n \le 7500$ | $O(n^2)$ | +| $n \le 7 \cdot 10^4$ | $O(n \sqrt n)$ | +| $n \le 5 \cdot 10^5$ | $O(n \log n)$ | +| $n \le 5 \cdot 10^6$ | $O(n)$ | +| $n \le 10^{18}$ | $O(\log^2 n)$, $O(\log n)$, $O(1)$ | ## Other Resources diff --git a/content/4_Silver/5_Silver_2P.md b/content/4_Silver/5_Silver_2P.md index 914ae42..1114465 100644 --- a/content/4_Silver/5_Silver_2P.md +++ b/content/4_Silver/5_Silver_2P.md @@ -8,7 +8,7 @@ prerequisites: - Silver - Sorting --- -**Two pointers** refers to iterating two monotonic pointers across an array to search for a pair of indices satisfying some condition in O(n). +**Two pointers** refers to iterating two monotonic pointers across an array to search for a pair of indices satisfying some condition in $O(n)$ time. @@ -27,11 +27,11 @@ prerequisites: - [Cellular Network](http://codeforces.com/problemset/problem/702/C) [](48) - [USB vs. PS/2](http://codeforces.com/problemset/problem/762/B) [](53) - [K-Good Segment](http://codeforces.com/problemset/problem/616/D) [](53) - - [(Long Title))](http://codeforces.com/problemset/problem/814/C) [](54) + - [(Long Title)](http://codeforces.com/problemset/problem/814/C) [](54) - [Jury Meeting](http://codeforces.com/problemset/problem/853/B) [](90) - USACO - [Gold Haybale Feast](http://usaco.org/index.php?page=viewproblem2&cpid=767) ## Extensions -The two pointers technique can be extended to the two dimensional sliding window algorithm. If you're looking for a more challenging problem using 2-D sliding window, see [USACO Plat Fort Moo](http://usaco.org/index.php?page=viewproblem2&cpid=600) \ No newline at end of file +The two pointers technique can be extended to the two dimensional sliding window algorithm. If you're looking for a more challenging problem using 2-D sliding window, see [USACO Plat Fort Moo](http://usaco.org/index.php?page=viewproblem2&cpid=600). \ No newline at end of file diff --git a/content/4_Silver/6_Silver_Psum.md b/content/4_Silver/6_Silver_Psum.md index 30611aa..fe0859d 100644 --- a/content/4_Silver/6_Silver_Psum.md +++ b/content/4_Silver/6_Silver_Psum.md @@ -5,7 +5,7 @@ author: Eric Wei (incomplete) order: 6 --- -Given an array of size $N$, answer $Q$ queries of the following form: Find the sum of all elements between indices $i$ and $j$. +> Given an array of size $N$, answer $Q$ queries of the following form: Find the sum of all elements between indices $i$ and $j$.