This commit is contained in:
Benjamin Qi 2020-06-08 21:41:23 -04:00
parent 486b06a350
commit 178308bbd5
5 changed files with 84 additions and 19 deletions

View file

@ -168,6 +168,65 @@ Although both Python and Java receive two times the C++ time limit in USACO, thi
</details>
<details>
<summary>Comparable C++ Solution (< 700ms)</summary>
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> 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;
}
```
</details>
## 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.

View file

@ -5,6 +5,10 @@ author: Benjamin Qi
order: 7
---
Shortening code and making it more (un?)readable.
<!-- END DESCRIPTION -->
## Introduction
- [GeeksForGeeks](https://www.geeksforgeeks.org/cc-preprocessors/#:~:text=Macros%3A%20Macros%20are%20a%20piece,used%20to%20define%20a%20macro.)

View file

@ -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

View file

@ -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.
<!-- END DESCRIPTION -->
@ -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)
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).

View file

@ -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$.
<!-- END DESCRIPTION -->