From Codeforces, Pierre Elliott Trudeau H.S., Olympiads School
About
Salar Wang '23
Spaces --> Tabs --> Spaces
TODO:
- https://dmoj.ca/problem/coci14c3p4
- https://dmoj.ca/problem/btoi13p5
- https://dmoj.ca/problem/dmopc17c2p3
- https://dmoj.ca/problem/dwite07c4p4
- https://dmoj.ca/problem/dwite10c3p4
- https://dmoj.ca/problem/xmascoordinates
- https://dmoj.ca/problem/ccc18j5
- https://dmoj.ca/problem/tsoc16c1p4
- https://dmoj.ca/problem/coci06c1p4
- https://dmoj.ca/problem/graph3p1
- https://dmoj.ca/problem/noi97p2
- https://dmoj.ca/problem/dmopc15c3p4
- https://dmoj.ca/problem/dmopc17c1p3
- https://dmoj.ca/problem/ccc17s4
- https://dmoj.ca/problem/dmopc16c4p2
- https://dmoj.ca/problem/dmopc18c5p2
- https://dmoj.ca/problem/dmopc19c5p4
- https://dmoj.ca/problem/ioi13p4
- https://dmoj.ca/problem/cco15p1
- https://dmoj.ca/problem/dmopc15c2p4
- https://dmoj.ca/problem/coci19c5p5
- https://dmoj.ca/problem/ioi98p6
First I'm grinding DM::OJ for some Algorithm and Data Structure knowledge, then I will migrate to Codeforces to train my ad-hoc skills.
Binary search is actually pretty hard :(
Delimiter
vector<string> split(string s, char delimiter) {
vector<string> tokens;
string token;
istringstream tokenStream(s);
while(getline(tokenStream, token, delimiter)) {
tokens.push_back(token);
}
return tokens;
}
Struct Sorting
Please watch out with using friend bool operator<(T &a, T &b)
. This code will produce the following with the Sample Input.
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int w;
friend bool operator<(const Edge &a, const Edge &b) {
return a.w > b.w;
}
};
int main() {
int n;
scanf("%i", &n);
vector<Edge> v(n);
priority_queue<Edge> q;
for(auto &x : v) {
scanf("%i", &x.w);
q.push(x);
}
printf("Priority Queue: ");
while(!q.empty()) {
auto cur = q.top(); q.pop();
printf("%i ", cur.w);
}
printf("\nSorting: ");
sort(begin(v), end(v));
for(auto &x : v) {
printf("%i ", x.w);
}
return 0;
}
Sample Input
20
51 11 21 79 36 61 9 61 39 85 37 30 37 40 79 16 95 72 24 51
Sample Output
Priority Queue: 9 11 16 21 24 30 36 37 37 39 40 51 51 61 61 72 79 79 85 95
Sorting: 95 85 79 79 72 61 61 51 51 40 39 37 37 36 30 24 21 16 11 9
Dijkstra's Algorithm
A little tip
priority_queue<pii, vector<pii>, greater<>> q;
q.push({d_s, s});
while(!q.empty()) {
auto cur = q.top(); q.pop(); // DO NOT USE auto &cur IT DOES NOT WORK
}
DMOPC '14 Exam Time P4 - Exam Delay
- The reason it's
ans * 60 * 1 / 3
is because (1) we convert from hours to minutes and (2) the speed is or the regular speed. Thus when find time by dividing the distance by the speed we multiply by the reciprocal so by . is the final time. To get the amount delayed we do - subtract by and we get . Thus the delayed amount is . - Make sure to store the answer before printing it in
scanf("%i\n", ans);
WC '15 Contest 1 S3 - Contest Sites
A few cases we consider after running Dijkstra's from cities -1
since there's no one there to not go, so we do not print -1
if
Mostly Talking
This is pretty similar to Convex Hull where you can use multi-dimension distance array where in Convex Hull its dist[total dist][hull taken]
while in Mostly Talking its simply dist[total dist][true/false called Cindy]
CCC '10 S4 - Animal Farm
The construction of the edge list for Kruskal's can be thought of as the following: For every connection (vector<pair<int, int>> dual[1005][1005]
) where int(dual[i][j].size()) == 2
) we can add the int(dual[i][j].size()) == 1
) since the animals can meet outside the barn. Run Kruskal's on the edge list with the newly added edges and store the result. Take the minimum of the two results and print your answer! :)
DMPG '15 S6 - Apples to Oranges
This question essentially pertains to the idea of a negative cycle. So, in order to detect said "negative cycle" we need to use an algorithm like Bellman-Ford. However, instead of using our classic relaxation formula of APPLES
more than YA
otherwise NAW
.
TLE '16 Contest 1 P4 - Microwaves Again!
For this problem we will first find all-pairs shortest path using an algorithm such as Floyd-Warshall's. Once we have precomputed all-pairs of shortest paths, we can create another matrix that determines whether the shortest path from
DMOPC '16 Contest 2 P4 - Zeros
We can observe the monotonic property of the number of zeros has with a factorial. Thus, since the number of trailing zeros is monotonic, we can perform binary search. In order to find the number of numbers that contain
WC '15 Finals S2 - Hydration
For this problem, we should notice that the amount of time it takes for a set of cows to finish is the maximum number of cows at a trough. Furthermore, we should notice that if all cows can finish drinking water in
CCC '10 S3 - Firehose
For this problem, we require binary search. We will binary search on the minimum length used
Dijkstra's
#include <bits/stdc++.h>
using namespace std;
const int MN = 1e5 + 5;
using pii = pair<int, int>;
vector<pii> adj[MN];
int dist[MN];
int main() {
int N, M, S, T;
cin >> N >> M;
for (int i = 0; i < M; ++i) {
int u, v, w;
cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
cin >> S >> T;
priority_queue<pii, vector<pii>, greater<>> pq;
memset(dist, 0x3f, sizeof dist);
pq.push({0, S});
dist[S] = 0;
while (!pq.empty()) {
auto cur = pq.top(); pq.pop();
int u = cur.second;
for (auto & [v, w] : adj[u]) {
int d = dist[u] + w;
if (d < dist[v]) {
dist[v] = d;
pq.push({d, v});
}
}
}
cout << dist[T] << endl;
return 0;
}
Dijkstra's
#include <bits/stdc++.h>
using namespace std;
const int MN = 1e5 + 5;
using pii = pair<int, int>;
int adj[MN][MN];
int dist[MN];
bool vis[MN];
int main() {
int N, M, S, T;
cin >> N >> M;
for (int i = 0; i < M; ++i) {
int u, v, w;
cin >> u >> v >> w;
adj[u][v] = adj[v][u] = w;
}
cin >> S >> T;
memset(dist, 0x3f, sizeof dist);
dist[S] = 0;
for (int it = 0; it < N; ++it) { // just iterate N times
int u = 0;
for (int i = 1; i <= N; ++i) { // find unvisited node with smallest dist
if (!vis[i] && dist[i] < dist[u]) {
u = i;
}
}
vis[u] = true;
for (int v = 1; v <= N; ++v) {
int d = dist[u] + adj[u][v];
if (!vis[v] && d < dist[v]) {
dist[v] = d;
}
}
}
cout << dist[T] << endl;
return 0;
}
Problems to re-do:
Interesting Problems:
- TLE '15 P5 - Prefix Sum Array (20/100)
- A Coin Game - I really like this problem because it still uses BFS but makes the nodes into a state (which coins are on which stack). Pretty cool!
To-do list (12/29):
- Review ✔️
- Math ✔️
- PSDA ✔️
- Graph ✔️
- BFS/DFS ✔️
- Dijkstra ✔️
- Prim's ✔️ (This is a pretty stupid algorithm)
- Disjoint Set & Kruskal's ✔️
- SPFA & Floyd Warshall ✔️
- Binary Search ✔️ (Continue practicing binary search on CF)
- Greedy 🟡 (I'll do some more on CF)
- Dynamic Programming ✔️
- Knapsack ✔️
- LIS ✔️
- String DP ✔️
- Interval DP ✔️
- Bitmask DP ✔️
- Geometry ✔️
- Polygon ✔️
- Linesweep
- Segment Tree
- Lazy Propagation
- Binary Indexed Tree
- Splay Tree
- Tarjan SCC
- Heavy-Light Decomposition
- Max flow
- Bipartite Matching
- Strings
56543baadad9454cbe86288dbd2e081ef12cb29a5c5bc8b89be64991b2cd91e8