├── Beginner Topics
├── README.md
├── [S1] Greed is good, sort of
│ ├── [EP 1] Implementation.md
│ ├── [EP 2] Greedy.md
│ ├── [EP 3] Sorting.md
│ ├── [EP 4] C++ STL.md
│ ├── [EP 5] Frequency tables.md
│ └── [~] Practice.md
├── [S2] Primal proficiency
│ ├── [EP 1] Basic math.md
│ ├── [EP 2] Primality, Factorization and Sieve.md
│ ├── [EP 3] GCD and Totient function.md
│ ├── [EP 4] Modular Arithmetic.md
│ ├── [EP 5] Binary Exponentiation.md
│ └── [~] Practice.md
├── [S3] 2P or not to 2P, No wait isn't this just bs
│ ├── [EP 1] Prefix sums.md
│ ├── [EP 2] Two pointers.md
│ ├── [EP 3] Binary search.md
│ ├── [EP 4] Ternary search.md
│ └── [~] Practice.md
├── [S4] Boohoo~ It's dp time
│ ├── [EP 0] Introduction to Dynamic Programming.md
│ ├── [EP 1] Coin change.md
│ ├── [EP 2] Maximum subarray sum.md
│ ├── [EP 3] Knapsack.md
│ ├── [EP 4] LCS & Edit distance.md
│ ├── [EP 5] LIS.md
│ ├── [EP 6] Grid DP.md
│ ├── [EP 7] MCM style DP.md
│ └── [~] Practice.md
├── [S5] Do you understand the graphity of this situation
│ ├── [EP 0] Graph terminology and representation.md
│ ├── [EP 1] Graph traversal
│ │ ├── [Pt 1] DFS.md
│ │ └── [Pt 2] BFS.md
│ ├── [EP 2] Shortest paths
│ │ ├── [Pt 1] Bellman-Ford.md
│ │ ├── [Pt 2] Dijkstra's.md
│ │ └── [Pt 3] Floyd-Warshall.md
│ └── [EP 3] Spanning Trees
│ │ ├── [Pt 1] Disjoint Set Union (Union Find).md
│ │ ├── [Pt 2] Kruskal's MST.md
│ │ └── [Pt 3] Prim's MST.md
└── [S6] Boohoo~ It's dp time (again)
│ └── to do.txt
├── C++ Tips and Tricks.md
├── CSES
├── Dynamic Programming
│ ├── Array Description.cpp
│ ├── Book Shop.cpp
│ ├── Coin Combinations I.cpp
│ ├── Coin Combinations II.cpp
│ ├── Counting Numbers.cpp
│ ├── Counting Tilings.cpp
│ ├── Counting Towers.cpp
│ ├── Dice Combinations.cpp
│ ├── Edit Distance.cpp
│ ├── Elevator Rides.cpp
│ ├── Grid Paths.cpp
│ ├── Increasing Subsequence.cpp
│ ├── Minimizing Coins.cpp
│ ├── Money Sums.cpp
│ ├── Projects.cpp
│ ├── Rectangle Cutting.cpp
│ ├── Removal Game.cpp
│ ├── Removing Digits.cpp
│ └── Two Sets II.cpp
├── Graph Algorithms
│ ├── Building Roads.cpp
│ ├── Building Teams.cpp
│ ├── Coin Collector.cpp
│ ├── Counting Rooms.cpp
│ ├── Course Schedule.cpp
│ ├── Cycle Finding.cpp
│ ├── Flight Discount.cpp
│ ├── Flight Routes.cpp
│ ├── Game Routes.cpp
│ ├── Giant Pizza.cpp
│ ├── Hamiltonian Flights.cpp
│ ├── High Score.cpp
│ ├── Investigation.cpp
│ ├── Labyrinth.cpp
│ ├── Longest Flight Route.cpp
│ ├── Mail Delivery.cpp
│ ├── Message Route.cpp
│ ├── Monsters.cpp
│ ├── Planets and Kingdoms.cpp
│ ├── Road Construction.cpp
│ ├── Road Reparation.cpp
│ ├── Round Trip II.cpp
│ ├── Round Trip.cpp
│ ├── Shortest Routes I.cpp
│ ├── Shortest Routes II.cpp
│ └── Teleporters Path.cpp
├── README.md
├── Sorting and Searching
│ ├── Apartments.cpp
│ ├── Array Division.cpp
│ ├── Collecting Numbers II.cpp
│ ├── Collecting Numbers.cpp
│ ├── Concert Tickets.cpp
│ ├── Distinct Numbers.cpp
│ ├── Factory Machines.cpp
│ ├── Ferris Wheel.cpp
│ ├── Josephus Problem I.cpp
│ ├── Josephus Problem II.cpp
│ ├── Maximum Subarray Sum II.cpp
│ ├── Maximum Subarray Sum.cpp
│ ├── Missing Coin Sum.cpp
│ ├── Movie Festival II.cpp
│ ├── Movie Festival.cpp
│ ├── Nearest Smaller Values.cpp
│ ├── Nested Ranges Check.cpp
│ ├── Nested Ranges Count.cpp
│ ├── Playlist.cpp
│ ├── Reading Books.cpp
│ ├── Restaurant Customers.cpp
│ ├── Room Allocation.cpp
│ ├── Sliding Cost.cpp
│ ├── Sliding Median.cpp
│ ├── Stick Lengths.cpp
│ ├── Subarray Distinct Values.cpp
│ ├── Subarray Divisibility.cpp
│ ├── Subarray Sums I.cpp
│ ├── Subarray Sums II.cpp
│ ├── Sum of Four Values.cpp
│ ├── Sum of Three Values.cpp
│ ├── Sum of Two Values.cpp
│ ├── Tasks and Deadlines.cpp
│ ├── Towers.cpp
│ └── Traffic Lights.cpp
└── Tree Algorithms
│ ├── Company Queries I.cpp
│ ├── Company Queries II.cpp
│ ├── Counting Paths.cpp
│ ├── Distance Queries.cpp
│ ├── Distinct Colors.cpp
│ ├── Matching.cpp
│ ├── Subordinates.cpp
│ ├── Subtree Queries.cpp
│ ├── Tree Diameter.cpp
│ ├── Tree Distances I.cpp
│ └── Tree Distances II.cpp
├── Competitve Programming Platforms.md
├── Complexity and Big-O Notation.md
├── Hello world.md
├── LICENSE
├── Macros and more.md
├── README.md
├── Sublime Text Setup.md
└── 末 Snippets
├── 0-k knapsack.cpp
├── 2-SAT.cpp
├── BFS.cpp
├── BIT (Fenwick tree).cpp
├── BigInt.cpp
├── Binary exponentiation.cpp
├── Binary search.cpp
├── Closest pair of points.cpp
├── DSU.cpp
├── Dijkstra.cpp
├── Edge_t.cpp
├── Euler Circuit (directed).cpp
├── Euler Circuit (undirected).cpp
├── Extended Euclidean algorithm.cpp
├── FFT.cpp
├── Factorization.cpp
├── FileIO (freopen).cpp
├── Floyd Warshall.cpp
├── Fractions & Rationals.cpp
├── GP hash table (unordered map alternative).cpp
├── Gaussian elimination.cpp
├── KMP.cpp
├── LCA using Binary Lifting (O(nlogn) \ O(logn)).cpp
├── LCA using RMQ (O(n) \ O(1)).cpp
├── LIS.cpp
├── Lambda comparators.cpp
├── MST (Kruskal's algo).cpp
├── MST (Prim's algo).cpp
├── Manachers.cpp
├── Matrix (std::array).cpp
├── Matrix (std::vector).cpp
├── Mo's Algorithm
├── Mo's algorithm on trees (weighted edges).cpp
├── Mo's algorithm on trees (weighted nodes).cpp
├── Mo's algorithm with updates.cpp
├── Mo's algorithm.cpp
├── Query struct (Canonical).cpp
└── Query struct (Hilbert ordering).cpp
├── ModInt.cpp
├── NTT.cpp
├── PBDS.cpp
├── Pollard rho.cpp
├── Random number generation.cpp
├── Remove duplicates.cpp
├── Run with larger stack size.cpp
├── SCC (Kosaraju's algo).cpp
├── SCC (Tarjan's algo).cpp
├── Segment trees
├── Iterative + Point updates (+ link to range updates).cpp
├── Recursive + Point updates.cpp
├── Recursive + Range updates (lazy prop).cpp
├── Recursive + persistent + point updates.cpp
└── Recursive + persistent + range updates (lazy prop).cpp
├── Sieve of Eratosthenes.cpp
├── Sparse Table.cpp
├── Spread queue (using two stacks).cpp
├── String hashing (Rolling|Polynomial hash).cpp
├── Topsort.cpp
├── Treap struct (with lazy prop).cpp
├── Treap.cpp
├── Y combinator.cpp
├── Z algo.cpp
├── k_II(cpp20).h
├── k_II.h
└── nCr.cpp
/Beginner Topics/[S1] Greed is good, sort of/[EP 1] Implementation.md:
--------------------------------------------------------------------------------
1 | # Ad hoc/Implementation
2 | Implementation problems do not rely on the knowledge of any particular data structure or algorithm. There isn't really any way to learn this method except to practice problems.
3 |
4 |
5 | Problems
6 |
19 |
20 |
21 |
22 |
23 | **Note:** Sometimes the input may be large enough to cause an overflow. So remember to use `long long` (64-bit integer) instead of `int` (32-bit integer). It is safe to assume anything above 2∙109 overflows `int`, and anything above 2∙1018 overflows `long long int`.
24 |
25 | Problems mentioned in this episode, as well as subsequent ones, are only meant to serve as an introduction to a topic and are by no means enough to make you an expert at it (unless you are a genius). Be sure to practice more problems on the topics. You may go to [Problemset](https://codeforces.com/problemset) on Codeforces and filter by tag and/or rating to find appropriate problems.
26 |
27 | Also note that most codeforces problems have editorials (explanations of solutions). You can find them on the bottom right, usually labelled 'Tutorial'. Read them if you are stuck.
28 |
--------------------------------------------------------------------------------
/Beginner Topics/[S1] Greed is good, sort of/[EP 2] Greedy.md:
--------------------------------------------------------------------------------
1 | # Greedy
2 | As the name suggests, a greedy approach is one where (greedily) picking the **best option** at every stage leads to the **best overall outcome**.
3 |
4 | **Reading material:**
5 | * [Hackerearth [B]](https://www.hackerearth.com/practice/algorithms/greedy/basics-of-greedy-algorithms/tutorial/)
6 |
7 |
8 | Problems
9 |
24 |
25 |
--------------------------------------------------------------------------------
/Beginner Topics/[S1] Greed is good, sort of/[EP 5] Frequency tables.md:
--------------------------------------------------------------------------------
1 | # Frequency tables
2 | Consider an array of elements. The frequency of a particular element is the number of times it occurs in the array. Calculating the frequencies of each element can be very useful to solve certain kinds of problems.
3 |
4 | Frequency tables are usually built on arrays or vectors, but can also be built on maps (or hash maps) when the range of values is too large to fit in memory.
5 | The tradeoff is that maps have $\mathcal{O}(log n)$ lookup time, unlike arrays and vectors which have $\mathcal{O}(1)$ lookup time (Hash maps have $\mathcal{O}(1)$ lookup time *on average*, not in the worst case).
6 |
7 | **Reading material:**
8 | * [GFG [B]](https://www.geeksforgeeks.org/counting-frequencies-of-array-elements/)
9 |
10 |
11 | Problems
12 |
18 |
19 |
20 |
21 |
22 | Challenging problems
23 |
27 |
--------------------------------------------------------------------------------
/Beginner Topics/[S1] Greed is good, sort of/[~] Practice.md:
--------------------------------------------------------------------------------
1 | # Practice
2 | Mashups of topic relevant problems spanning a range of difficulties.
3 |
4 |
5 | Mashup 1
6 |
20 |
21 |
22 |
23 | Mashup 2
24 |
38 |
--------------------------------------------------------------------------------
/Beginner Topics/[S2] Primal proficiency/[EP 1] Basic math.md:
--------------------------------------------------------------------------------
1 | # Basic math
2 |
3 |
4 | Problems
5 |
17 |
--------------------------------------------------------------------------------
/Beginner Topics/[S2] Primal proficiency/[EP 2] Primality, Factorization and Sieve.md:
--------------------------------------------------------------------------------
1 | # Primality, Factorization and Sieve of Eratosthenes
2 | Efficient factorization and prime checking is a must know when it comes to competitive programming. This is the place to start for number theory.
3 |
4 | **Reading material:**
5 | * Introduction - *CPH Ch 21: Number theory,* pg [197](https://cses.fi/book/book.pdf#page=207) - [198](https://cses.fi/book/book.pdf#page=208)
6 | * Primality and Prime factorization - *CPH Ch 21,* pg [199](https://cses.fi/book/book.pdf#page=209)
7 | * Sieve of Eratosthenes
8 | * *CPH Ch 21,* pg [200](https://cses.fi/book/book.pdf#page=210)
9 | * [YouTube [V]](https://www.youtube.com/watch?v=pKvGYOnO9Ao)
10 | * [Visualization [B]](https://brilliant.org/wiki/sieve-of-eratosthenes/)
11 | * Prime factorization queries - [Tutorial [B]](https://www.geeksforgeeks.org/prime-factorization-using-sieve-olog-n-multiple-queries/)
12 | * Prime gap - [Article [B]](https://primes.utm.edu/notes/gaps.html)
13 |
14 |
15 | Problems
16 |
25 |
26 |
27 |
28 |
29 | Challenging problems
30 |
34 |
35 |
--------------------------------------------------------------------------------
/Beginner Topics/[S2] Primal proficiency/[EP 3] GCD and Totient function.md:
--------------------------------------------------------------------------------
1 | # Euclidean GCD and Euler's Totient function
2 | You'll see a fair number of easy mathy problems whose solutions involve finding and using the Greatest Common Divisor (GCD) of some numbers. The Euler totient function is rarer, but it is still good to know.
3 |
4 | **Reading material:**
5 | * Euclid's algorithm
6 | * *CPH Ch 21,* pg [200](https://cses.fi/book/book.pdf#page=210)
7 | * [CP Algorithms](https://cp-algorithms.com/algebra/euclid-algorithm.html)
8 | * Euler's totient function
9 | * *CPH Ch 21,* pg [201](https://cses.fi/book/book.pdf#page=211)
10 | * [CP Algorithms](https://cp-algorithms.com/algebra/phi-function.html)
11 | * Topcoder article - [click](https://www.topcoder.com/thrive/articles/Prime%20Numbers,%20Factorization%20and%20Euler%20Function)
12 |
13 |
14 | Problems
15 |
24 |
25 |
26 |
27 | Challenging problems
28 |
36 |
37 |
38 |
39 | **Tip**: Use [std::gcd](https://en.cppreference.com/w/cpp/numeric/gcd) or **__gcd()** (a builtin function of the GCC compiler).
40 |
--------------------------------------------------------------------------------
/Beginner Topics/[S2] Primal proficiency/[EP 5] Binary Exponentiation.md:
--------------------------------------------------------------------------------
1 |
2 | # Binary Exponentiation
3 | Binary exponentiation (also known as exponentiation by squaring) is a trick which allows you to calculate $a^n$ using only $\mathcal{O}(logn)$ multiplications (instead of $\mathcal{O}(n)$ multiplications).
4 |
5 | Of course, this also works under mod. This is sometimes referred to as Modular Exponentiation.
6 |
7 | **Reading material:**
8 | * [Errichto [V]](https://www.youtube.com/watch?v=L-Wzglnm4dM)
9 | * [CP Algorithms [B]](https://cp-algorithms.com/algebra/binary-exp.html)
10 |
11 |
12 | Problems
13 |
17 |
--------------------------------------------------------------------------------
/Beginner Topics/[S2] Primal proficiency/[~] Practice.md:
--------------------------------------------------------------------------------
1 | # Practice
2 | Mashups of topic relevant problems spanning a range of difficulties.
3 |
4 |
5 | Mashup 1
6 |
18 |
19 |
20 |
21 | Mashup 2
22 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/Beginner Topics/[S3] 2P or not to 2P, No wait isn't this just bs/[EP 1] Prefix sums.md:
--------------------------------------------------------------------------------
1 | # Prefix sums
2 | Given an array $a$ of $n$ elements, prefix sums allow you to calculate the sum of any range of elements $[l, r]$ $1 \leq l \leq r \leq n$ in $\mathcal{O}(1)$ constant time (with $\mathcal{O}(n)$ pre-processing).
3 |
4 | The trick is to pre-calculate (to calculate once beforehand for multiple usages later) an array $\mathrm{prefix}$, of cumulative sums. For each $i$ from $1$ to $n$, $\mathrm{prefix}_i$ stores the sum $a_1 + a_2 + \ldots + a_i$. Then, the sum of elements in the range $[l, r]$ can be easily found out as $\mathrm{prefix}_r - \mathrm{prefix}_{l-1}$.
5 |
6 | **Reading material:**
7 | * [Peltorator [V]](https://codeforces.com/blog/entry/88474) (Use english subtitles)
8 | * [USACO [B]](https://usaco.guide/silver/prefix-sums?lang=cpp)
9 | * 2D prefix sums - [USACO [B]](https://usaco.guide/silver/more-prefix-sums?lang=cpp)
10 | * Range updates using prefix sums - [Codeforces [B]](https://codeforces.com/blog/entry/86420)
11 |
12 |
13 | Problems
14 |
26 |
27 |
28 |
29 | *This page uses math latex formatting. Download the [extension](https://chrome.google.com/webstore/detail/github-math-display/cgolaobglebjonjiblcjagnpmdmlgmda) to render it.*
30 |
--------------------------------------------------------------------------------
/Beginner Topics/[S3] 2P or not to 2P, No wait isn't this just bs/[EP 2] Two pointers.md:
--------------------------------------------------------------------------------
1 | # Two pointers and sliding window
2 | The two pointer or 2p technique, is an op technique that has the power to reduce an $\mathcal{O}(n^2)$ algorithm to an $\mathcal{O}(n)$ one.
3 |
4 | **Reading material:**
5 | * Two pointers - [Quora [B]](https://tp-iiita.quora.com/The-Two-Pointer-Algorithm) & [CF EDU](https://codeforces.com/edu/course/2/lesson/9)
6 | * Sliding window - [GFG [B]](https://www.geeksforgeeks.org/window-sliding-technique/)
7 | * Sliding window applications - [Medium [B]](https://medium.com/techie-delight/top-problems-on-sliding-window-technique-8e63f1e2b1fa)
8 |
9 |
10 | Problems
11 |
23 |
24 |
--------------------------------------------------------------------------------
/Beginner Topics/[S3] 2P or not to 2P, No wait isn't this just bs/[EP 4] Ternary search.md:
--------------------------------------------------------------------------------
1 | # Ternary Search
2 | Ternary Search is an extension of binary Search that allows you to locate the minima or maxima of a [unimodal function](https://en.wikipedia.org/wiki/Unimodality#:~:text=A%20common%20definition%20is%20as,Proving%20unimodality%20is%20often%20hard.).
3 |
4 | **Reading material:**
5 | * [CP Algorithms [B]](https://cp-algorithms.com/num_methods/ternary_search.html) - try reading this. Very detailed explanation.
6 | * [HackerEarth [B]](https://www.hackerearth.com/practice/algorithms/searching/ternary-search/tutorial/)
7 |
8 |
9 | Problems
10 |
17 |
18 |
--------------------------------------------------------------------------------
/Beginner Topics/[S4] Boohoo~ It's dp time/[EP 0] Introduction to Dynamic Programming.md:
--------------------------------------------------------------------------------
1 | # Introduction to Dynamic Programming
2 |
3 |
4 | Dynamic programming (abbreviated DP), is an approach to solving problems through 'clever bruteforce'. It is a technique that combines the correctness of complete search and the efficiency of greedy algorithms. Dynamic programming can
5 | be applied if the problem can be divided into overlapping subproblems that can be solved independently.
6 |
7 | There are two common uses for dynamic programming:
8 | * **Finding an optimal solution**: You want to find a solution that is as large as possible or as small as possible.
9 | * **Counting the number of solutions**: You want to calculate the total number of possible solutions.
10 |
11 | If you're following this series, DP could be quite a jump in terms of difficulty and thinking of approaches to problems. Take your time to assimilate the contents of the following links/guides and understand what's going on. The internet has a lot more resources to explore if you're stuck.
12 |
13 | The links that follow are good to start with, but you will most probably have to revisit and re-read them multiple times as you struggle through the components that follow.
14 |
15 | **Reading material:**
16 | * [Errichto DP Lecture #1 [V]](https://www.youtube.com/watch?v=YBSt1jYwVfU)
17 | * [Topcoder [B]](https://www.topcoder.com/community/competitive-programming/tutorials/dynamic-programming-from-novice-to-advanced/)
18 | * [Hackerearth [B]](https://www.hackerearth.com/practice/algorithms/dynamic-programming/introduction-to-dynamic-programming-1/tutorial/)
19 | * [Blog [B]](https://skerritt.blog/dynamic-programming/)
20 |
21 |
22 | Problems
23 |
28 |
29 |
30 |
31 | **Note:** Solutions to [AtCoder DP contest](https://atcoder.jp/contests/dp/tasks) problems can be found [here](https://www.youtube.com/watch?v=FAQxdm0bTaw).
--------------------------------------------------------------------------------
/Beginner Topics/[S4] Boohoo~ It's dp time/[EP 1] Coin change.md:
--------------------------------------------------------------------------------
1 | # Coin change
2 |
3 | **Reading material:**
4 | * [Errichto DP Lecture #2 [V]](https://www.youtube.com/watch?v=1mtvm2ubHCY)
5 | * *CPH Ch 7,* pg [65](https://cses.fi/book/book.pdf#page=75) - [70](https://cses.fi/book/book.pdf#page=80)
6 | * [Blog [B]](https://hackernoon.com/the-coin-change-problem-explained-ddd035a8f22f)
7 |
8 |
9 | Problems
10 |
16 |
17 |
18 |
19 | **Note:** Solutions to [CSES DP section](https://atcoder.jp/contests/dp/tasks) problems can be found [here](https://codeforces.com/blog/entry/70018).
--------------------------------------------------------------------------------
/Beginner Topics/[S4] Boohoo~ It's dp time/[EP 2] Maximum subarray sum.md:
--------------------------------------------------------------------------------
1 | # Maximum sum subarray (Kadane's algorithm)
2 |
3 | **Reading material:**
4 | * [Medium [B]](https://medium.com/@rsinghal757/kadanes-algorithm-dynamic-programming-how-and-why-does-it-work-3fd8849ed73d)
5 | * *CPH Ch 2,* pg [21](https://cses.fi/book/book.pdf#page=31) - [23](https://cses.fi/book/book.pdf#page=33)
6 |
7 |
8 | Problems
9 |
15 |
--------------------------------------------------------------------------------
/Beginner Topics/[S4] Boohoo~ It's dp time/[EP 3] Knapsack.md:
--------------------------------------------------------------------------------
1 | # Knapsack
2 |
3 | **Reading material:**
4 | * [Algorithms Live [V]](https://www.youtube.com/watch?v=U4O3SwDamA4)
5 | * *CPH Ch 7,* pg [72](https://cses.fi/book/book.pdf#page=82) - [73](https://cses.fi/book/book.pdf#page=83)
6 | * [HackerEarth](https://www.hackerearth.com/practice/notes/the-knapsack-problem/)
7 |
8 |
9 | Problems
10 |
18 |
19 |
20 |
21 |
22 | Challenging problems
23 |
28 |
--------------------------------------------------------------------------------
/Beginner Topics/[S4] Boohoo~ It's dp time/[EP 4] LCS & Edit distance.md:
--------------------------------------------------------------------------------
1 | # Longest Common Subsequence & Edit distance
2 |
3 | **Reading material:**
4 | * Longest Common Subsequence
5 | * [Blog [B]](https://www.programiz.com/dsa/longest-common-subsequence)
6 | * [Errichto [V]](https://www.youtube.com/watch?v=KJWAQVmuFW0)
7 | * Edit distance
8 | * [Blog [B]](https://www.techiedelight.com/levenshtein-distance-edit-distance-problem/)
9 | * *CPH Ch 7,* pg [74](https://cses.fi/book/book.pdf#page=84)
10 |
11 |
12 |
13 | Problems
14 |
21 |
--------------------------------------------------------------------------------
/Beginner Topics/[S4] Boohoo~ It's dp time/[EP 5] LIS.md:
--------------------------------------------------------------------------------
1 | # Longest Increasing Subsequence
2 | The dp approach is $\mathcal{O}(n^2)$ and helps build intuition and understanding. However, since there exists methods to solve it in O(nlogn), often you will not be able to use this approach.
3 |
4 | **Reading material:**
5 | * $\mathcal{O}(n^2)$
6 | * *CPH Ch 7,* pg [70](https://cses.fi/book/book.pdf#page=80)
7 | * $\mathcal{O}(nlogn)$
8 | * [USACO guide](https://usaco.guide/gold/lis?lang=cpp)
9 | * [CP Algorithms](https://cp-algorithms.com/sequences/longest_increasing_subsequence.html)
10 |
11 |
12 | Problems
13 |
21 |
--------------------------------------------------------------------------------
/Beginner Topics/[S4] Boohoo~ It's dp time/[EP 6] Grid DP.md:
--------------------------------------------------------------------------------
1 | # Grid DP
2 |
3 | **Reading material:**
4 | * [Hackerearth [B]](https://www.hackerearth.com/practice/notes/dynamic-programming-problems-involving-grids/)
5 | * *CPH Ch 7,* pg [71](https://cses.fi/book/book.pdf#page=81) - [72](https://cses.fi/book/book.pdf#page=82)
6 | * [Errichto [V]](https://www.youtube.com/watch?v=NL12nchRoUE)
7 |
8 |
9 | Problems
10 |
15 |
--------------------------------------------------------------------------------
/Beginner Topics/[S4] Boohoo~ It's dp time/[EP 7] MCM style DP.md:
--------------------------------------------------------------------------------
1 | # Matrix Chain Multiplication style DP
2 |
3 | **Reading material:**
4 | * *Introduction to Algorithms, CLRS* pg [370](https://web.iiit.ac.in/~pratik.kamble/storage/Algorithms/Cormen_Algorithms_3rd.pdf#page=391)
5 | * [Blog [B]](https://www.techiedelight.com/matrix-chain-multiplication/)
6 | * [Video [V]](https://www.youtube.com/watch?v=prx1psByp7U)
7 |
8 |
9 | Problems
10 |
17 |
--------------------------------------------------------------------------------
/Beginner Topics/[S5] Do you understand the graphity of this situation/[EP 0] Graph terminology and representation.md:
--------------------------------------------------------------------------------
1 | # Introduction to Graph theory
2 | Many programming problems can be solved by modeling them as graph
3 | problems and using appropriate graph algorithms. A typical example of a graph
4 | is a network of roads and cities in a country. Sometimes though, the graph is
5 | hidden in the problem and it may be difficult to detect it.
6 |
7 | **Reading material:**
8 | * *CPH Ch 11,* pg [109](https://cses.fi/book/book.pdf#page=119) - [116](https://cses.fi/book/book.pdf#page=126)
9 | * [Hackerearth [B]](https://www.hackerearth.com/practice/algorithms/graphs/graph-representation/tutorial/)
10 | * A [collection](https://codeforces.com/blog/entry/16221) of several graph algorithms
11 | * Basic Problems - [E-Olymp Graph representation](https://www.e-olymp.com/en/contests/9060)
--------------------------------------------------------------------------------
/Beginner Topics/[S5] Do you understand the graphity of this situation/[EP 1] Graph traversal/[Pt 1] DFS.md:
--------------------------------------------------------------------------------
1 | ## Depth First Search (DFS)
2 |
3 | **Reading material:**
4 | * *CPH Ch 12,* pg [117](https://cses.fi/book/book.pdf#page=127) - [118](https://cses.fi/book/book.pdf#page=128)
5 | * [Hackerearth [B]](https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/tutorial/)
6 | * [CP Algorithms](https://cp-algorithms.com/graph/depth-first-search.html)
7 | * Basic Problems - [E-Olymp DFS](https://www.e-olymp.com/en/contests/9116)
8 | * DFS Applications
9 | * *CPH Ch 12,* pg [121](https://cses.fi/book/book.pdf#page=131) - [122](https://cses.fi/book/book.pdf#page=132)
10 | * [-is-this-fft-'s legendary blog on CF](https://codeforces.com/blog/entry/68138) (for later reading)
11 |
12 |
13 | Problems
14 |
26 |
--------------------------------------------------------------------------------
/Beginner Topics/[S5] Do you understand the graphity of this situation/[EP 1] Graph traversal/[Pt 2] BFS.md:
--------------------------------------------------------------------------------
1 | ## Breadth First Search (BFS)
2 |
3 | **Reading material:**
4 | * *CPH Ch 12,* pg [119](https://cses.fi/book/book.pdf#page=129) - [120](https://cses.fi/book/book.pdf#page=130)
5 | * [Hackerearth [B]](https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/tutorial/)
6 | * [CP Algorithms](https://cp-algorithms.com/graph/breadth-first-search.html)
7 | * Basic Problems - [E-Olymp BFS](https://www.e-olymp.com/en/contests/9208)
8 |
9 |
10 | Problems
11 |
21 |
22 |
23 |
24 | **See also:** [0-1 BFS](https://cp-algorithms.com/graph/01_bfs.html) | [Alt](https://codeforces.com/blog/entry/22276)
25 |
26 |
27 | 0-1 BFS Problems
28 |
33 |
--------------------------------------------------------------------------------
/Beginner Topics/[S5] Do you understand the graphity of this situation/[EP 2] Shortest paths/[Pt 1] Bellman-Ford.md:
--------------------------------------------------------------------------------
1 | # Bellman-Ford Algorithm
2 |
3 | **Reading material:**
4 | * *CPH Ch 13,* pg [123](https://cses.fi/book/book.pdf#page=133) - [126](https://cses.fi/book/book.pdf#page=136)
5 | * [CP Algorithms](https://cp-algorithms.com/graph/bellman_ford.html)
6 |
7 |
8 | Problems
9 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/Beginner Topics/[S5] Do you understand the graphity of this situation/[EP 2] Shortest paths/[Pt 2] Dijkstra's.md:
--------------------------------------------------------------------------------
1 | # Dijkstra's Algorithm
2 |
3 | **Reading material:**
4 | * *CPH Ch 13,* pg [126](https://cses.fi/book/book.pdf#page=136) - [129](https://cses.fi/book/book.pdf#page=139)
5 | * CP Algorithms
6 | * [Dijkstra Algorithm](https://cp-algorithms.com/graph/dijkstra.html)
7 | * [Dijkstra on sparse graphs](https://cp-algorithms.com/graph/dijkstra_sparse.html)
8 |
9 |
10 | Problems
11 |
23 |
--------------------------------------------------------------------------------
/Beginner Topics/[S5] Do you understand the graphity of this situation/[EP 2] Shortest paths/[Pt 3] Floyd-Warshall.md:
--------------------------------------------------------------------------------
1 | # Floyd-Warshall Algorithm
2 |
3 | **Reading material:**
4 | * *CPH Ch 13,* pg [129](https://cses.fi/book/book.pdf#page=139) - [131](https://cses.fi/book/book.pdf#page=141)
5 | * [CP Algorithms](https://cp-algorithms.com/graph/all-pair-shortest-path-floyd-warshall.html)
6 |
7 |
8 | Problems
9 |
16 |
--------------------------------------------------------------------------------
/Beginner Topics/[S5] Do you understand the graphity of this situation/[EP 3] Spanning Trees/[Pt 1] Disjoint Set Union (Union Find).md:
--------------------------------------------------------------------------------
1 | # Disjoint Set Union or Union Find
2 |
3 | **Reading material:**
4 | * *CPH Ch 15,* pg [145](https://cses.fi/book/book.pdf#page=155) - [147](https://cses.fi/book/book.pdf#page=157)
5 | * [CS Academy [B]](https://csacademy.com/lesson/disjoint_data_sets/)
6 | * [CP Algorithms](https://cp-algorithms.com/data_structures/disjoint_set_union.html)
7 | * CF EDU on [Disjoint Set Union](https://codeforces.com/edu/course/2/lesson/7)
8 |
9 |
10 | Problems
11 |
22 |
--------------------------------------------------------------------------------
/Beginner Topics/[S5] Do you understand the graphity of this situation/[EP 3] Spanning Trees/[Pt 2] Kruskal's MST.md:
--------------------------------------------------------------------------------
1 | # Kruskal's Algorithm
2 |
3 | **Reading material:**
4 | * *CPH Ch 15,* pg [141](https://cses.fi/book/book.pdf#page=151) - [145](https://cses.fi/book/book.pdf#page=155)
5 | * CP Algorithms
6 | * [Kruskal's Algorithm](https://cp-algorithms.com/graph/mst_kruskal.html)
7 | * [Kruskal with DSU](https://cp-algorithms.com/graph/mst_kruskal_with_dsu.html)
8 |
9 |
10 | Problems
11 |
21 |
--------------------------------------------------------------------------------
/Beginner Topics/[S5] Do you understand the graphity of this situation/[EP 3] Spanning Trees/[Pt 3] Prim's MST.md:
--------------------------------------------------------------------------------
1 | # Prim's Algorithm
2 |
3 | **Reading material:**
4 | * *CPH Ch 15,* pg [147](https://cses.fi/book/book.pdf#page=157) - [148](https://cses.fi/book/book.pdf#page=158)
5 | * [CP Algorithms](https://cp-algorithms.com/graph/mst_prim.html)
6 |
7 |
8 | Problems
9 |
19 |
20 |
--------------------------------------------------------------------------------
/Beginner Topics/[S6] Boohoo~ It's dp time (again)/to do.txt:
--------------------------------------------------------------------------------
1 | Bitmask dp
2 | Tree dp
3 | SOS dp
4 | Matrix Exponentiation
5 |
6 | * [Bitmasks](https://www.hackerearth.com/practice/algorithms/dynamic-programming/bit-masking/tutorial/)
7 | * [Sum over subsets (SoS)](https://codeforces.com/blog/entry/45223)
8 | * [DP on trees](https://codeforces.com/blog/entry/20935)
9 |
10 | ## Matrix exponentiation
11 | * [Modular arithmetic, combinatorics and exponentiation](https://codeforces.com/blog/entry/78873)
12 |
13 | https://codeforces.com/blog/entry/93914
14 |
15 | [Tutorial](http://zobayer.blogspot.com/2010/11/matrix-exponentiation.html)\
16 | [Another good read](https://codeforces.com/blog/entry/67776)\
17 | [Errichto video](https://www.youtube.com/watch?v=eMXNWcbw75E_)\
18 | [Optimized to O(k^2 logN) using Cayley Hamilton theorem](https://discuss.codechef.com/t/linear-recurrence-using-cayley-hamilton-theorem/6776)
19 |
20 | #### Problems
21 | * [Spoj FIBOFAST](https://www.spoj.com/problems/FIBOFAST/)
22 | * [SPOJ Mpow](https://www.spoj.com/problems/MPOW/)
23 | * [SPOJ FiboSum](https://www.spoj.com/problems/FIBOSUM/cstart=10)
24 | * [Codeforces 185 A](https://codeforces.com/contest/185/problem/A)
25 | * [Codeforces 166 E](https://codeforces.com/problemset/problem/166/E)
26 | * [Codeforces 1117 D](https://codeforces.com/problemset/problem/1117/D)
27 | * [Mat expo training contest (Errichto)](https://codeforces.com/gym/102644)
--------------------------------------------------------------------------------
/CSES/Dynamic Programming/Array Description.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.12.2020 18:26:28
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define sz(x) int(x.size())
13 | #define all(x) x.begin(), x.end()
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 1e5 + 5;
18 | const int M = 100 + 5;
19 | int64_t dp[N][M], a[N];
20 |
21 | int main() {
22 | cin.tie(nullptr)->sync_with_stdio(false);
23 | int i, j, n, m;
24 | cin >> n >> m;
25 | for(i = 0; i < n; i++)
26 | cin >> a[i];
27 |
28 | if(!a[0]) fill(dp[0]+1, dp[0]+m+1, 1);
29 | else dp[0][a[0]] = 1;
30 |
31 | for(i = 1; i < n; i++) {
32 | if(a[i]) {
33 | dp[i][a[i]] = dp[i-1][a[i]-1] + dp[i-1][a[i]] + dp[i-1][a[i]+1];
34 | dp[i][a[i]] %= MOD;
35 | } else {
36 | for(j = 1; j <= m; j++) {
37 | dp[i][j] = dp[i-1][j-1] + dp[i-1][j] + dp[i-1][j+1];
38 | dp[i][j] %= MOD;
39 | }
40 | }
41 | } cout << accumulate(dp[n-1], dp[n-1]+m+1, 0ll) % MOD;
42 | } // ~W
--------------------------------------------------------------------------------
/CSES/Dynamic Programming/Book Shop.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.12.2020 18:21:37
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define sz(x) int(x.size())
13 | #define all(x) x.begin(), x.end()
14 | auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
15 | auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
16 |
17 | const int64_t DESPACITO = 2e18;
18 | const int INF = 2e9, MOD = 1e9+7;
19 | const int N = 2e5 + 5;
20 | int dp[N], cost[N], val[N];
21 |
22 | int main() {
23 | cin.tie(nullptr)->sync_with_stdio(false);
24 | int i, j, n, S;
25 | cin >> n >> S;
26 | for(i = 0; i < n; i++)
27 | cin >> cost[i];
28 | for(i = 0; i < n; i++)
29 | cin >> val[i];
30 |
31 | for(i = 0; i < n; i++)
32 | for(j = S; j >= cost[i]; --j)
33 | chmax(dp[j], dp[j-cost[i]] + val[i]);
34 |
35 | cout << *max_element(dp, dp+S+1);
36 | } // ~W
--------------------------------------------------------------------------------
/CSES/Dynamic Programming/Coin Combinations I.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.12.2020 17:31:52
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define sz(x) int(x.size())
13 | #define all(x) x.begin(), x.end()
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 1e6 + 5;
18 | int c[N], dp[N];
19 |
20 | int main() {
21 | cin.tie(nullptr)->sync_with_stdio(false);
22 | int i, j, n, S;
23 | cin >> n >> S;
24 | for(i = 0; i < n; i++)
25 | cin >> c[i];
26 | sort(c, c+n);
27 |
28 | dp[0] = 1;
29 | for(i = 0; i < S; i++)
30 | for(j = 0; j < n; j++)
31 | if(i + c[j] <= S)
32 | (dp[i+c[j]] += dp[i]) %= MOD;
33 |
34 | cout << dp[S];
35 | } // ~W
36 |
37 |
38 | /* Recursive~ */
39 |
40 | /**
41 | 🍪 the_hyp0cr1t3
42 | 🍪 12.01.2021 15:45:41
43 | **/
44 | #ifdef W
45 | #include "k_II.h"
46 | #else
47 | #include
48 | using namespace std;
49 | #endif
50 | #define pb emplace_back
51 | #define all(x) x.begin(), x.end()
52 | #define sz(x) static_cast(x.size())
53 |
54 | const int64_t DESPACITO = 2e18;
55 | const int INF = 2e9, MOD = 1e9+7;
56 | const int N = 100 + 5;
57 | const int M = 1e6 + 5;
58 | int weights[N], cache[M];
59 |
60 | int dp(int S) {
61 | if(S < 0) return 0;
62 | int& ans = cache[S];
63 | if(~ans) return ans;
64 | ans = 0;
65 | for(int i = 0; weights[i] <= S; i++) {
66 | ans += dp(S - weights[i]);
67 | if(ans >= MOD) ans -= MOD;
68 | } return ans;
69 | }
70 |
71 | int main() {
72 | cin.tie(nullptr)->sync_with_stdio(false);
73 | int i, n, S;
74 | cin >> n >> S;
75 | for(i = 0; i < n; i++)
76 | cin >> weights[i];
77 | sort(weights, weights + n);
78 | weights[n] = INF;
79 | memset(cache, -1, sizeof(cache));
80 | cache[0] = 1;
81 | cout << dp(S);
82 | } // ~W
--------------------------------------------------------------------------------
/CSES/Dynamic Programming/Coin Combinations II.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 thew6rst
3 | 🍪 11.02.2021 17:06:56
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 101;
18 | const int M = 1e6 + 5;
19 | int dp[N][M];
20 |
21 | int main() {
22 | cin.tie(nullptr)->sync_with_stdio(false);
23 | int i, j, n, k;
24 | cin >> n >> k;
25 | vector a(n);
26 | for(auto& x: a) cin >> x;
27 |
28 | dp[0][0] = 1;
29 | for(i = 1; i <= n; i++) {
30 | for(j = 0; j <= k; j++) {
31 | dp[i][j] = dp[i-1][j];
32 | int remain = j - a[i-1];
33 | if(remain >= 0) {
34 | dp[i][j] += dp[i][remain];
35 | if(dp[i][j] >= MOD)
36 | dp[i][j] -= MOD;
37 | }
38 | }
39 | }
40 |
41 | cout << dp[n][k];
42 | } // ~W
--------------------------------------------------------------------------------
/CSES/Dynamic Programming/Counting Numbers.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 02.02.2021 23:55:33
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 | int64_t dp[20][2][10];
19 |
20 | int main() {
21 | cin.tie(nullptr)->sync_with_stdio(false);
22 | int64_t A, B;
23 | cin >> A >> B;
24 |
25 | auto go = [](int64_t n) {
26 | stringstream ss; string s;
27 | ss << n; ss >> s; reverse(all(s));
28 | int64_t ans = 0;
29 | memset(dp, 0, sizeof(dp));
30 | dp[0][0][0] = dp[0][1][0] = dp[1][0][0] = dp[1][1][0] = 1;
31 | for(int len = 1; len <= sz(s); len++) {
32 | for(int j = 0; j < 10; j++) {
33 | for(int k = 0; k < 10; k++) {
34 | dp[len][0][j] += j ^ k? dp[len-1][0][k] : 0;
35 | if(j == s[len-1]-'0') {
36 | dp[len][1][j] += k ^ j? dp[len-1][1][k] : 0;
37 | } else if(j < s[len-1]-'0') {
38 | dp[len][1][j] += j ^ k? dp[len-1][0][k] : 0;
39 | }
40 | } if(len == 1 or j) ans += dp[len][len == sz(s)][j];
41 | }
42 | } return ans;
43 | };
44 |
45 | cout << go(B) - (A > 0? go(A - 1) : 0);
46 |
47 | } // ~W
--------------------------------------------------------------------------------
/CSES/Dynamic Programming/Dice Combinations.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.12.2020 17:26:32
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define sz(x) int(x.size())
13 | #define all(x) x.begin(), x.end()
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 1e6 + 10;
18 | int dp[N][6];
19 |
20 | int main() {
21 | cin.tie(nullptr)->sync_with_stdio(false);
22 | int i, j, k, n;
23 | cin >> n;
24 |
25 | dp[0][0] = 1;
26 | for(i = 0; i < n; i++)
27 | for(j = 0; j < 6; j++)
28 | for(k = 0; k < 6; k++)
29 | (dp[i+k+1][k] += dp[i][j]) %= MOD;
30 |
31 | int ans = 0;
32 | for(j = 0; j < 6; j++)
33 | (ans += dp[n][j]) %= MOD;
34 | cout << ans;
35 | } // ~W
--------------------------------------------------------------------------------
/CSES/Dynamic Programming/Edit Distance.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.12.2020 18:37:28
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define sz(x) int(x.size())
13 | #define all(x) x.begin(), x.end()
14 | auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
15 | auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
16 |
17 | const int64_t DESPACITO = 2e18;
18 | const int INF = 2e9, MOD = 1e9+7;
19 | const int N = 5000 + 5;
20 | int dp[N][N];
21 |
22 | int main() {
23 | cin.tie(nullptr)->sync_with_stdio(false);
24 | int i, j, n, m; string s, t;
25 | cin >> s >> t; n = sz(s); m = sz(t);
26 |
27 | for(i = 0; i <= n; i++)
28 | for(j = 0; j <= m; j++)
29 | dp[i][j] = INF;
30 | dp[0][0] = 0;
31 |
32 | for(i = 0; i <= n; i++) {
33 | for(j = 0; j <= m; j++) {
34 | if(i) chmin(dp[i][j], dp[i-1][j] + 1);
35 | if(j) chmin(dp[i][j], dp[i][j-1] + 1);
36 | if(i and j) chmin(dp[i][j], dp[i-1][j-1] + (s[i-1] != t[j-1]));
37 | }
38 | }
39 |
40 | cout << dp[n][m];
41 | } // ~W
--------------------------------------------------------------------------------
/CSES/Dynamic Programming/Elevator Rides.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 02.02.2021 23:25:43
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 | auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
15 | auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
16 |
17 | const int64_t DESPACITO = 2e18;
18 | const int INF = 2e9, MOD = 1e9+7;
19 | const int N = 20;
20 | int a[N];
21 | array, 1 << N> dp;
22 |
23 | int main() {
24 | cin.tie(nullptr)->sync_with_stdio(false);
25 | int i, n, S;
26 | cin >> n >> S;
27 | for(i = 0; i < n; i++)
28 | cin >> a[i];
29 |
30 | dp.fill({INF, INF}); dp[0] = {1, 0};
31 | for(int msk = 1; msk < 1<>i&1) {
34 | auto cand = dp[msk ^ 1< S)
37 | cand[0]++, cand[1] = a[i];
38 | chmin(dp[msk], cand);
39 | }
40 | }
41 | } cout << dp[(1<
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define sz(x) int(x.size())
13 | #define all(x) x.begin(), x.end()
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 1000 + 5;
18 | int dp[N][N];
19 |
20 | int main() {
21 | cin.tie(nullptr)->sync_with_stdio(false);
22 | int i, j, n;
23 | cin >> n;
24 | vector a(n);
25 | for(auto& x: a) cin >> x;
26 |
27 | dp[1][0] = a[0][0] == '.';
28 | for(i = 1; i <= n; i++)
29 | for(j = 1; j <= n; j++)
30 | if(a[i-1][j-1] == '.') dp[i][j] = (dp[i-1][j] + dp[i][j-1]) % MOD;
31 |
32 | cout << dp[n][n];
33 | } // ~W
--------------------------------------------------------------------------------
/CSES/Dynamic Programming/Increasing Subsequence.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 thew6rst
3 | 🍪 11.02.2021 17:18:28
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, n;
22 | cin >> n;
23 | vector a(n);
24 | for(auto& x: a) cin >> x;
25 |
26 | auto LIS = [&] (const vector& v) {
27 | vector lis; lis.reserve(n);
28 | for(int z = 0; z < n; z++) {
29 | // v[A] <= v[B] for strictly incr, v[A] < v[B] otherwise
30 | auto it = upper_bound(all(lis), z, [&](const auto& A, const auto& B) {
31 | return v[A] <= v[B];
32 | });
33 | int idx = int(it - lis.begin());
34 | if(it == lis.end()) lis.pb(z);
35 | else lis[idx] = z;
36 | } return sz(lis);
37 | };
38 |
39 | cout << LIS(a);
40 | } // ~W
--------------------------------------------------------------------------------
/CSES/Dynamic Programming/Minimizing Coins.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.12.2020 17:31:52
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define sz(x) int(x.size())
13 | #define all(x) x.begin(), x.end()
14 | auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
15 | auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
16 |
17 | const int64_t DESPACITO = 2e18;
18 | const int INF = 2e9, MOD = 1e9+7;
19 | const int N = 1e6 + 5;
20 | int c[N], dp[N];
21 |
22 | int main() {
23 | cin.tie(nullptr)->sync_with_stdio(false);
24 | int i, j, n, S;
25 | cin >> n >> S;
26 | for(i = 0; i < n; i++)
27 | cin >> c[i];
28 | sort(c, c+n);
29 |
30 | for(i = 0; i <= S; i++)
31 | dp[i] = INF;
32 | dp[0] = 0;
33 |
34 | for(i = 0; i <= S; i++)
35 | for(j = 0; j < n; j++)
36 | if(i + c[j] <= S)
37 | chmin(dp[i+c[j]], dp[i] + 1);
38 |
39 | cout << (dp[S] == INF? -1 : dp[S]);
40 | } // ~W
--------------------------------------------------------------------------------
/CSES/Dynamic Programming/Money Sums.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.12.2020 18:50:14
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define sz(x) int(x.size())
13 | #define all(x) x.begin(), x.end()
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 1e5 + 5;
18 | int dp[N+1];
19 |
20 | int main() {
21 | cin.tie(nullptr)->sync_with_stdio(false);
22 | int i, j, n;
23 | cin >> n;
24 | for(dp[i = 0] = 1; i < n; i++) {
25 | int x; cin >> x;
26 | for(j = N-1; j >= x; --j)
27 | if(dp[j-x]) dp[j] = true;
28 | }
29 |
30 | cout << accumulate(dp+1, dp+N, 0) << '\n';
31 | for(i = 1; i < N; i++)
32 | if(dp[i]) cout << i << ' ';
33 | } // ~W
--------------------------------------------------------------------------------
/CSES/Dynamic Programming/Projects.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 10.11.2020 11:41:01
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define sz(x) int(x.size())
13 | #define all(x) x.begin(), x.end()
14 | auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
15 | auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
16 |
17 | const int64_t DESPACITO = 2e18;
18 | const int INF = 2e9, MOD = 1e9+7;
19 | const int N = 2e5 + 5;
20 |
21 | int main() {
22 | cin.tie(nullptr)->sync_with_stdio(false);
23 | int i, j, n, m = 1, cur = 0;
24 | cin >> n;
25 | map hsh;
26 | vector> a(n);
27 | for(i = 0; i < n; i++) {
28 | for(j = 0; j < 3; j++)
29 | cin >> a[i][j];
30 | hsh[a[i][0]]; hsh[a[i][1]];
31 | }
32 |
33 | for(auto& [k, v]: hsh)
34 | v = m++;
35 | for(auto& [l, r, c]: a)
36 | l = hsh[l], r = hsh[r];
37 | sort(all(a));
38 |
39 | vector dp(m+1);
40 | for(auto& [l, r, c]: a) {
41 | while(cur < l) chmax(dp[cur+1], dp[cur]), ++cur;
42 | chmax(dp[r], dp[l-1] + c);
43 | } while(cur < m) chmax(dp[cur+1], dp[cur]), ++cur;
44 |
45 | cout << dp[m] << '\n';
46 | } // ~W
--------------------------------------------------------------------------------
/CSES/Dynamic Programming/Rectangle Cutting.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.12.2020 18:44:16
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define sz(x) int(x.size())
13 | #define all(x) x.begin(), x.end()
14 | auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
15 | auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
16 |
17 | const int64_t DESPACITO = 2e18;
18 | const int INF = 2e9, MOD = 1e9+7;
19 | const int N = 500 + 5;
20 | int dp[N][N];
21 |
22 | int main() {
23 | cin.tie(nullptr)->sync_with_stdio(false);
24 | int i, j, k, a, b;
25 | cin >> a >> b;
26 |
27 | for(i = 1; i <= a; i++) {
28 | for(j = 1; j <= b; j++) {
29 | if(i == j) continue;
30 | dp[i][j] = INF;
31 | for(k = 1; k < i; k++)
32 | chmin(dp[i][j], dp[k][j] + dp[i-k][j] + 1);
33 | for(k = 1; k < j; k++)
34 | chmin(dp[i][j], dp[i][k] + dp[i][j-k] + 1);
35 | }
36 | } cout << dp[a][b];
37 | } // ~W
--------------------------------------------------------------------------------
/CSES/Dynamic Programming/Removal Game.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 31.08.2020 10:14:30
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #define aylmao cin.tie(nullptr)->sync_with_stdio(false);
11 | #endif
12 | #define endl '\n'
13 | #define pb emplace_back
14 | // #define int long long
15 | #define sz(x) (int)((x).size())
16 | #define all(x) x.begin(), x.end()
17 | #define Luv(...) [&](auto&& u, auto&& v) { return __VA_ARGS__; }
18 | auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
19 | auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
20 |
21 | using ll = long long;
22 | using pii = pair;
23 | const ll DESPACITO = 2e18;
24 | const int INF = 2e9, MOD = 1e9+7;
25 |
26 | const int N = 2e5+5;
27 | int a[N];
28 |
29 | int32_t main() {
30 | aylmao
31 | int i, n, cur = 1; ll ans = 0;
32 | cin >> n;
33 | for(i = 0; i < n; i++)
34 | cin >> a[i];
35 | deque v;
36 | for(i = 0; i < n; i++) {
37 | v.push_back(a[i]);
38 | while(sz(v) > 2) {
39 | ll x = v[sz(v)-3];
40 | ll y = v[sz(v)-2];
41 | ll z = v[sz(v)-1];
42 | if(x > y or y < z) break;
43 | v.pop_back(); v.pop_back(); v.pop_back();
44 | v.push_back(x+z-y);
45 | }
46 | }
47 | while(!v.empty()) {
48 | ans += max(v.front(), v.back()) * cur;
49 | if(v.front() > v.back()) v.pop_front();
50 | else v.pop_back();
51 | cur = -cur;
52 | }
53 | cout << (accumulate(a, a+n, 0ll) + ans >> 1);
54 | return 0;
55 | }
--------------------------------------------------------------------------------
/CSES/Dynamic Programming/Removing Digits.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.12.2020 18:05:04
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define sz(x) int(x.size())
13 | #define all(x) x.begin(), x.end()
14 | auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
15 | auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
16 |
17 | const int64_t DESPACITO = 2e18;
18 | const int INF = 2e9, MOD = 1e9+7;
19 | const int N = 1e6 + 5;
20 | int dp[N];
21 |
22 | int main() {
23 | cin.tie(nullptr)->sync_with_stdio(false);
24 | int i, n;
25 | cin >> n;
26 |
27 | fill(dp, dp+n, INF);
28 | for(i = n; i; --i) {
29 | int x = i;
30 | while(x) chmin(dp[i-x%10], dp[i] + 1), x /= 10;
31 | } cout << dp[0];
32 | } // ~W
--------------------------------------------------------------------------------
/CSES/Dynamic Programming/Two Sets II.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.12.2020 19:40:29
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define sz(x) int(x.size())
13 | #define all(x) x.begin(), x.end()
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 | int dp[N];
19 |
20 | int main() {
21 | cin.tie(nullptr)->sync_with_stdio(false);
22 | int i, j, n;
23 | cin >> n;
24 | int sum = n * (n+1) >> 1;
25 |
26 | if(sum & 1) return cout << 0, 0;
27 | sum >>= 1;
28 |
29 | dp[0] = 1;
30 | for(i = 1; i <= n; i++)
31 | for(j = sum; j >= i; --j)
32 | (dp[j] += dp[j-i]) %= MOD;
33 |
34 | cout << (500000004ll * dp[sum] % MOD);
35 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Building Roads.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 17.05.2021 22:26:38
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t k_II = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, n, m;
22 | cin >> n >> m;
23 |
24 | vector par(n, -1);
25 | auto get_par = [&par](int A) {
26 | while(par[A] >= 0) {
27 | if(par[par[A]] >= 0) par[A] = par[par[A]];
28 | A = par[A];
29 | } return A;
30 | };
31 |
32 | auto merge = [&get_par, &par](int A, int B) {
33 | A = get_par(A); B = get_par(B);
34 | if(A == B) return false;
35 | if(-par[A] < -par[B]) swap(A, B);
36 | par[A] += par[B]; par[B] = A;
37 | return true;
38 | };
39 |
40 | while(m--) {
41 | int u, v; cin >> u >> v;
42 | merge(--u, --v);
43 | }
44 |
45 | int x = -1;
46 | vector> ans;
47 | for(i = 0; i < n; i++) {
48 | if(par[i] < 0) {
49 | if(~x) ans.pb(x, i);
50 | x = i;
51 | }
52 | }
53 |
54 | cout << sz(ans) << '\n';
55 | for(auto& [x, y]: ans)
56 | cout << x+1 << ' ' << y+1 << '\n';
57 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Building Teams.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 17.05.2021 22:46:57
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t k_II = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, n, m;
22 | cin >> n >> m;
23 |
24 | vector par(n, -1), val(n);
25 | auto get_par = [&par, &val](int A) {
26 | int v = 0;
27 | while(par[A] >= 0) {
28 | if(par[par[A]] >= 0) {
29 | val[A] ^= val[par[A]];
30 | par[A] = par[par[A]];
31 | } v ^= val[A], A = par[A];
32 | } return pair{ A, v ^ val[A] };
33 | };
34 |
35 | auto merge = [&get_par, &par, &val](int _A, int _B) {
36 | auto[A, a] = get_par(_A);
37 | auto [B, b] = get_par(_B);
38 | if(A == B) return a ^ b;
39 | if(-par[A] < -par[B]) swap(A, B);
40 | par[A] += par[B]; par[B] = A;
41 | val[B] = a ^ b ^ 1;
42 | return 1;
43 | };
44 |
45 | while(m--) {
46 | int u, v; cin >> u >> v;
47 | if(!merge(--u, --v))
48 | return cout << "IMPOSSIBLE" << '\n', 0;
49 | }
50 |
51 | for(i = 0; i < n; i++)
52 | cout << get_par(i).second + 1 << " \n"[i == n-1];
53 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Counting Rooms.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 17.05.2021 21:55:07
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | template class Y {
16 | T f;
17 | public:
18 | template explicit Y(U&& f): f(forward(f)) {}
19 | template decltype(auto) operator()(Args&&... args) {
20 | return f(ref(*this), forward(args)...);
21 | }
22 | }; template Y(T) -> Y;
23 |
24 | const array dx{1, 0, -1, 0};
25 | const array dy{0, 1, 0, -1};
26 |
27 | const int64_t k_II = 2e18;
28 | const int INF = 2e9, MOD = 1e9+7;
29 | const int N = 2e5 + 5;
30 |
31 | int main() {
32 | cin.tie(nullptr)->sync_with_stdio(false);
33 | int i, j, n, m, ans = 0;
34 | cin >> n >> m;
35 | vector a(n);
36 | for(auto& x: a) cin >> x;
37 |
38 | auto valid = [&](int x, int y) {
39 | return 0 <= x and x < n and 0 <= y and y < m and a[x][y] == '.';
40 | };
41 |
42 | vector> vis(n, vector(m));
43 | auto dfs = Y([&](auto dfs, int x, int y) -> void {
44 | vis[x][y] = 1;
45 | for(int z = 0; z < 4; z++) {
46 | int nx = x + dx[z];
47 | int ny = y + dy[z];
48 | if(valid(nx, ny) and !vis[nx][ny])
49 | dfs(nx, ny);
50 | }
51 | });
52 |
53 | for(i = 0; i < n; i++)
54 | for(j = 0; j < m; j++)
55 | if(!vis[i][j] and a[i][j] == '.')
56 | dfs(i, j), ans++;
57 |
58 | cout << ans << endl;
59 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Course Schedule.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 19.05.2021 18:22:59
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t k_II = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, n, m;
22 | cin >> n >> m;
23 | vector> g(n);
24 | vector indeg(n);
25 | for(i = 0; i < m; i++) {
26 | int u, v; cin >> u >> v; u--, v--;
27 | g[u].pb(v); indeg[v]++;
28 | }
29 |
30 | // topsort
31 | queue q;
32 | vector order; order.reserve(n);
33 | for(i = 0; i < n; i++)
34 | if(!indeg[i]) q.push(i);
35 | while(!q.empty()) {
36 | auto v = q.front(); q.pop();
37 | order.pb(v);
38 | for(auto& x: g[v])
39 | if(!--indeg[x]) q.push(x);
40 | }
41 |
42 | if(sz(order) < n)
43 | return cout << "IMPOSSIBLE" << '\n', 0;
44 |
45 | for(auto& x: order)
46 | cout << x+1 << ' ';
47 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Cycle Finding.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 18.05.2021 11:18:52
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 | auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
15 | auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
16 |
17 | struct edge_t {
18 | int u{-1}, v{-1}; int64_t w{0};
19 | edge_t() = default;
20 | edge_t(int u, int v, int64_t w = 0) : u(u), v(v), w(w) {}
21 | bool operator<(const edge_t& other) { return w < other.w; }
22 | friend ostream& operator<<(ostream& os, const edge_t& e) { os << e.u << ' ' << e.v << ' ' << e.w; return os; }
23 | };
24 |
25 | const int64_t k_II = 2e18;
26 | const int INF = 2e9, MOD = 1e9+7;
27 | const int N = 2e5 + 5;
28 |
29 | int main() {
30 | cin.tie(nullptr)->sync_with_stdio(false);
31 | int i, n, m, x;
32 | cin >> n >> m;
33 | vector edges(m);
34 | for(auto& e: edges) {
35 | cin >> e.u >> e.v >> e.w;
36 | --e.u; --e.v;
37 | }
38 |
39 | bool changed;
40 | vector p(n, -1);
41 | vector d(n);
42 | for(i = 0; i < n; i++) {
43 | changed = false;
44 | for(auto& e: edges)
45 | if(chmin(d[e.v], d[e.u] + e.w))
46 | changed = true, x = e.v, p[e.v] = e.u;
47 | }
48 |
49 | if(!changed) return cout << "NO" << '\n', 0;
50 |
51 | for(i = 0; i < n; i++) x = p[x];
52 | vector ans; ans.reserve(n);
53 | i = x;
54 | do { ans.pb(i); i = p[i]; } while(i ^ x);
55 | ans.pb(i);
56 | reverse(all(ans));
57 |
58 | cout << "YES" << '\n';
59 | for(auto& x: ans)
60 | cout << x+1 << ' ';
61 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Flight Discount.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 18.05.2021 01:42:23
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 | auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
15 | auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
16 |
17 | const int64_t k_II = 2e18;
18 | const int INF = 2e9, MOD = 1e9+7;
19 | const int N = 2e5 + 5;
20 |
21 | int main() {
22 | cin.tie(nullptr)->sync_with_stdio(false);
23 | int i, n, m;
24 | cin >> n >> m;
25 | vector>> g(n);
26 | for(i = 0; i < m; i++) {
27 | int u, v, w; cin >> u >> v >> w;
28 | g[--u].pb(--v, w);
29 | }
30 |
31 | struct state {
32 | int v, o; int64_t dist;
33 | state(int v, int o, int64_t dist): v(v), o(o), dist(dist) {}
34 | bool operator<(const state& o) const {
35 | return dist > o.dist;
36 | }
37 | };
38 |
39 | array, 2> d;
40 | d[0] = d[1] = vector(n, k_II);
41 | priority_queue pq;
42 | pq.emplace(0, 0, d[0][0] = 0);
43 | while(!pq.empty()) {
44 | state top = pq.top(); pq.pop();
45 | if(top.dist > d[top.o][top.v]) continue;
46 | for(auto& [to, w]: g[top.v]) {
47 | if(chmin(d[top.o][to], top.dist + w))
48 | pq.emplace(to, top.o, d[top.o][to]);
49 | if(!top.o and chmin(d[1][to], top.dist + w/2))
50 | pq.emplace(to, 1, d[1][to]);
51 | }
52 | }
53 |
54 | cout << min(d[0].back(), d[1].back());
55 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Flight Routes.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.09.2021 00:24:44
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 | auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
15 | auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
16 |
17 | const int64_t k_II = 2e18;
18 | const int INF = 2e9, MOD = 1e9+7;
19 | const int N = 2e5 + 5;
20 |
21 | int main() {
22 | ios_base::sync_with_stdio(false), cin.tie(nullptr);
23 | int i, n, m, k;
24 | cin >> n >> m >> k;
25 | vector>> g(n);
26 | for(i = 0; i < m; i++) {
27 | int u, v, w; cin >> u >> v >> w;
28 | g[--u].pb(--v, w);
29 | }
30 |
31 | struct state {
32 | int v; int64_t dist;
33 | state(int v, int64_t dist): v(v), dist(dist) {}
34 | bool operator<(const state& o) const {
35 | return dist > o.dist;
36 | }
37 | };
38 |
39 | vector d(n, vector(k, k_II));
40 | priority_queue pq;
41 | pq.emplace(0, d[0][0] = 0);
42 | while(!pq.empty()) {
43 | state top = pq.top(); pq.pop();
44 | if(top.dist > d[top.v][k - 1]) continue;
45 | for(auto& [to, w]: g[top.v])
46 | if(chmin(d[to][k - 1], top.dist + w))
47 | pq.emplace(to, top.dist + w), sort(all(d[to]));
48 | }
49 |
50 | for(auto& x: d[n - 1])
51 | cout << x << ' ';
52 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Game Routes.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 19.05.2021 18:22:59
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 | auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
15 | auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
16 |
17 | const int64_t k_II = 2e18;
18 | const int INF = 2e9, MOD = 1e9+7;
19 | const int N = 2e5 + 5;
20 |
21 | int main() {
22 | cin.tie(nullptr)->sync_with_stdio(false);
23 | int i, n, m;
24 | cin >> n >> m;
25 | vector> g(n);
26 | vector indeg(n);
27 | for(i = 0; i < m; i++) {
28 | int u, v; cin >> u >> v; u--, v--;
29 | g[u].pb(v); indeg[v]++;
30 | }
31 |
32 | queue q;
33 | vector order; order.reserve(n);
34 | for(i = 0; i < n; i++)
35 | if(!indeg[i]) q.push(i);
36 | while(!q.empty()) {
37 | auto v = q.front(); q.pop();
38 | order.pb(v);
39 | for(auto& x: g[v])
40 | if(!--indeg[x]) q.push(x);
41 | }
42 |
43 | vector dp(n); dp[0] = 1;
44 | for(auto& v: order) {
45 | for(auto& x: g[v]) {
46 | dp[x] += dp[v];
47 | if(dp[x] >= MOD)
48 | dp[x] -= MOD;
49 | }
50 | }
51 |
52 | cout << dp[n-1];
53 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Hamiltonian Flights.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.09.2021 20:31:44
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t k_II = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | ios_base::sync_with_stdio(false), cin.tie(nullptr);
21 | int i, j, n, m;
22 | cin >> n >> m;
23 | vector adj(n, vector(n));
24 | while(m--) {
25 | int u, v; cin >> u >> v;
26 | adj[--u][--v]++;
27 | }
28 |
29 | vector dp(1 << n, vector(n));
30 | dp[1][0] = 1;
31 |
32 | for(int mask = 1; mask < 1 << n; mask++) {
33 | for(i = 0; i < n; i++) if(mask >> i & 1 and dp[mask][i]) {
34 | for(j = 1; j < n; j++) if(~mask >> j & 1 and adj[i][j]) {
35 | dp[mask | 1 << j][j] += dp[mask][i] * adj[i][j];
36 | dp[mask | 1 << j][j] %= MOD;
37 | }
38 | }
39 | }
40 |
41 | cout << dp[(1 << n) - 1][n - 1];
42 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Investigation.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 28.08.2021 00:10:58
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 | auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
15 | auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
16 |
17 | const int64_t k_II = 2e18;
18 | const int INF = 2e9, MOD = 1e9+7;
19 | const int N = 2e5 + 5;
20 |
21 | int main() {
22 | ios_base::sync_with_stdio(false), cin.tie(nullptr);
23 | int i, n, m;
24 | cin >> n >> m;
25 | vector>> g(n);
26 | for(i = 0; i < m; i++) {
27 | int u, v, w; cin >> u >> v >> w;
28 | g[--u].pb(--v, w);
29 | }
30 |
31 | struct state {
32 | int v; int64_t dist;
33 | state(int v, int64_t dist): v(v), dist(dist) {}
34 | bool operator<(const state& o) const {
35 | return dist > o.dist;
36 | }
37 | };
38 |
39 | vector ways(n), mn(n), mx(n);
40 | ways[0] = 1;
41 |
42 | vector d(n, k_II);
43 | priority_queue pq;
44 | pq.emplace(0, d[0] = 0);
45 |
46 | while(!pq.empty()) {
47 | state top = pq.top(); pq.pop();
48 | if(top.dist > d[top.v]) continue;
49 | for(auto& [to, w]: g[top.v]) {
50 | if(d[to] == top.dist + w) {
51 | ways[to] += ways[top.v];
52 | if(ways[to] >= MOD) ways[to] -= MOD;
53 | mn[to] = min(mn[to], mn[top.v] + 1);
54 | mx[to] = max(mx[to], mx[top.v] + 1);
55 | } else if(chmin(d[to], top.dist + w)) {
56 | ways[to] = ways[top.v];
57 | mn[to] = mn[top.v] + 1; mx[to] = mx[top.v] + 1;
58 | pq.emplace(to, d[to]);
59 | }
60 | }
61 | }
62 |
63 | cout << d.back() << ' ' << ways.back() << ' ' << mn.back() << ' ' << mx.back();
64 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Labyrinth.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 17.05.2021 22:03:26
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const array dx{1, 0, -1, 0};
16 | const array dy{0, 1, 0, -1};
17 | const string dir = "ULDR";
18 |
19 | const int64_t k_II = 2e18;
20 | const int INF = 2e9, MOD = 1e9+7;
21 | const int N = 2e5 + 5;
22 |
23 | int main() {
24 | cin.tie(nullptr)->sync_with_stdio(false);
25 | int i, j, n, m, stx, sty, ex, ey;
26 | cin >> n >> m;
27 | vector a(n);
28 | for(auto& x: a) cin >> x;
29 |
30 | for(i = 0; i < n; i++)
31 | for(j = 0; j < m; j++)
32 | if(a[i][j] == 'A')
33 | stx = i, sty = j;
34 | else if(a[i][j] == 'B')
35 | ex = i, ey = j;
36 |
37 |
38 | auto valid = [&](int x, int y) {
39 | return 0 <= x and x < n and 0 <= y and y < m and a[x][y] != '#';
40 | };
41 |
42 | vector> d(n, vector(m, -1));
43 | vector> p(n, vector(m, -1));
44 | queue> q;
45 | d[stx][sty] = 0; q.emplace(stx, sty);
46 |
47 | while(!q.empty()) {
48 | auto [x, y] = q.front(); q.pop();
49 | for(int z = 0; z < 4; z++) {
50 | int nx = x + dx[z];
51 | int ny = y + dy[z];
52 | if(valid(nx, ny) and d[nx][ny] == -1) {
53 | d[nx][ny] = d[x][y] + 1;
54 | p[nx][ny] = z + 2 & 3;
55 | q.emplace(nx, ny);
56 | }
57 | }
58 | }
59 |
60 | if(d[ex][ey] == -1)
61 | return cout << "NO" << '\n', 0;
62 |
63 | string ans;
64 | while(ex != stx or ey != sty) {
65 | ans += dir[p[ex][ey]];
66 | tie(ex, ey) = pair{ex + dx[p[ex][ey]], ey + dy[p[ex][ey]]};
67 | } reverse(all(ans));
68 |
69 | cout << "YES" << '\n';
70 | cout << sz(ans) << '\n';
71 | cout << ans << '\n';
72 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Longest Flight Route.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.09.2021 00:33:57
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 | auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
15 | auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
16 |
17 | const int64_t k_II = 2e18;
18 | const int INF = 2e9, MOD = 1e9+7;
19 | const int N = 2e5 + 5;
20 |
21 | int main() {
22 | ios_base::sync_with_stdio(false), cin.tie(nullptr);
23 | int i, n, m;
24 | cin >> n >> m;
25 | vector indeg(n);
26 | vector> g(n);
27 | for(i = 0; i < m; i++) {
28 | int u, v; cin >> u >> v;
29 | g[--u].pb(--v); indeg[v]++;
30 | }
31 |
32 | vector par(n, -1), dp(n, -INF);
33 | dp[0] = 1;
34 |
35 | queue q;
36 | for(i = 0; i < n; i++)
37 | if(!indeg[i]) q.push(i);
38 |
39 | while(!q.empty()) {
40 | auto v = q.front(); q.pop();
41 | for(auto& x: g[v]) {
42 | if(!--indeg[x]) q.push(x);
43 | if(chmax(dp[x], dp[v] + 1)) par[x] = v;
44 | }
45 | }
46 |
47 | if(dp[n - 1] < 0)
48 | return cout << "IMPOSSIBLE" << '\n', 0;
49 |
50 | vector ans;
51 | ans.reserve(dp[n - 1]);
52 | for(i = n-1; ~i; i = par[i]) ans.pb(i);
53 | reverse(all(ans));
54 |
55 | cout << sz(ans) << '\n';
56 | for(auto& x: ans) cout << x + 1 << ' ';
57 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Mail Delivery.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.03.2021 19:01:10
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | template class Y {
16 | T f;
17 | public:
18 | template explicit Y(U&& f): f(forward(f)) {}
19 | template decltype(auto) operator()(Args&&... args) {
20 | return f(ref(*this), forward(args)...);
21 | }
22 | }; template Y(T) -> Y;
23 |
24 | struct edge_t {
25 | int u{-1}, v{-1}; int64_t w{0}; bool done{false};
26 | edge_t() = default;
27 | edge_t(int u, int v, int64_t w = 0) : u(u), v(v), w(w) {}
28 | bool operator<(const edge_t& other) { return w < other.w; }
29 | friend ostream& operator<<(ostream& os, const edge_t& e) { os << e.u << ' ' << e.v << ' ' << e.w; return os; }
30 | };
31 |
32 | const int64_t DESPACITO = 2e18;
33 | const int INF = 2e9, MOD = 1e9+7;
34 | const int N = 2e5 + 5;
35 |
36 | int main() {
37 | cin.tie(nullptr)->sync_with_stdio(false);
38 | int i, n, m;
39 | cin >> n >> m;
40 |
41 | vector deg(n);
42 | vector edges;
43 | edges.reserve(m);
44 | vector> g(n);
45 |
46 | for(i = 0; i < m; i++) {
47 | int u, v; cin >> u >> v;
48 | deg[--u]++; deg[--v]++;
49 | edges.pb(u, v);
50 | g[u].pb(&edges.back());
51 | g[v].pb(&edges.back());
52 | }
53 |
54 | bool bad = false;
55 | for(i = 0; i < n; i++)
56 | bad |= deg[i] & 1;
57 |
58 | if(bad)
59 | return cout << "IMPOSSIBLE" << '\n', 0;
60 |
61 | vector tour;
62 | tour.reserve(m+1);
63 | Y([&](auto dfs, int v) -> void {
64 | while(deg[v]-- > 0) {
65 | auto e = g[v][deg[v]];
66 | if(!e->done)
67 | e->done = true, dfs(e->u ^ e->v ^ v);
68 | } tour.pb(v);
69 | })(0);
70 |
71 | for(i = 0; i < n; i++)
72 | if(deg[i] > 0)
73 | return cout << "IMPOSSIBLE" << '\n', 0;
74 |
75 | reverse(all(tour));
76 | for(auto& x: tour) cout << ++x << ' ';
77 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Message Route.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 17.05.2021 22:39:20
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t k_II = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, n, m;
22 | cin >> n >> m;
23 | vector> g(n);
24 | for(i = 0; i < m; i++) {
25 | int u, v; cin >> u >> v; u--, v--;
26 | g[u].pb(v); g[v].pb(u);
27 | }
28 |
29 | queue q; q.push(0);
30 | vector d(n, -1), p(n, -1); d[0] = 0;
31 | while(!q.empty()) {
32 | auto cur = q.front(); q.pop();
33 | for(auto nxt : g[cur]) {
34 | if(d[nxt] == -1) {
35 | d[nxt] = d[cur] + 1;
36 | p[nxt] = cur;
37 | q.push(nxt);
38 | }
39 | }
40 | }
41 |
42 | if(d.back() == -1)
43 | return cout << "IMPOSSIBLE" << '\n', 0;
44 |
45 | i = n-1;
46 | vector ans; ans.reserve(d.back());
47 | while(~i) ans.pb(exchange(i, p[i]));
48 | reverse(all(ans));
49 |
50 | cout << d.back()+1 << '\n';
51 | for(auto x: ans) cout << x+1 << ' ';
52 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Road Construction.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.09.2021 01:01:32
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t k_II = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | ios_base::sync_with_stdio(false), cin.tie(nullptr);
21 | int n, m, mx = 1;
22 | cin >> n >> m;
23 |
24 | vector par(n + 1, -1);
25 | auto get_par = [&par](int A) {
26 | while(par[A] >= 0) {
27 | if(par[par[A]] >= 0)
28 | par[A] = par[par[A]];
29 | A = par[A];
30 | } return A;
31 | };
32 |
33 | auto merge = [&get_par, &par](int A, int B) {
34 | A = get_par(A); B = get_par(B);
35 | if(A == B) return false;
36 | if(-par[A] < -par[B]) swap(A, B);
37 | par[A] += par[B]; par[B] = A;
38 | return true;
39 | };
40 |
41 | while(m--) {
42 | int u, v; cin >> u >> v;
43 | if(merge(u, v)) n--;
44 | mx = max(mx, -par[get_par(u)]);
45 | cout << n << ' ' << mx << '\n';
46 | }
47 |
48 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Road Reparation.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.09.2021 00:55:27
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | struct edge_t {
16 | int u{-1}, v{-1}; int64_t w{0};
17 | edge_t() = default;
18 | edge_t(int u, int v, int64_t w = 0) : u(u), v(v), w(w) {}
19 | bool operator<(const edge_t& other) { return w < other.w; }
20 | };
21 |
22 | const int64_t k_II = 2e18;
23 | const int INF = 2e9, MOD = 1e9+7;
24 | const int N = 2e5 + 5;
25 |
26 | int main() {
27 | ios_base::sync_with_stdio(false), cin.tie(nullptr);
28 | int n, m, cnt = 0;
29 | cin >> n >> m;
30 |
31 | vector edges(m);
32 | for(auto& e: edges)
33 | cin >> e.u >> e.v >> e.w;
34 | sort(all(edges));
35 |
36 | vector par(n + 1, -1);
37 | auto get_par = [&par](int A) {
38 | while(par[A] >= 0) {
39 | if(par[par[A]] >= 0)
40 | par[A] = par[par[A]];
41 | A = par[A];
42 | } return A;
43 | };
44 |
45 | auto merge = [&get_par, &par](int A, int B) {
46 | A = get_par(A); B = get_par(B);
47 | if(A == B) return false;
48 | if(-par[A] < -par[B]) swap(A, B);
49 | par[A] += par[B]; par[B] = A;
50 | return true;
51 | };
52 |
53 | int64_t ans = 0;
54 | for(auto& e: edges)
55 | if(merge(e.u, e.v))
56 | ans += e.w, cnt++;
57 |
58 | if(cnt < n - 1)
59 | cout << "IMPOSSIBLE" << '\n';
60 | else
61 | cout << ans << '\n';
62 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Round Trip II.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 18.05.2021 12:06:35
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | template class Y {
16 | T f;
17 | public:
18 | template explicit Y(U&& f): f(forward(f)) {}
19 | template decltype(auto) operator()(Args&&... args) {
20 | return f(ref(*this), forward(args)...);
21 | }
22 | }; template Y(T) -> Y;
23 |
24 | const int64_t k_II = 2e18;
25 | const int INF = 2e9, MOD = 1e9+7;
26 | const int N = 2e5 + 5;
27 |
28 | int main() {
29 | cin.tie(nullptr)->sync_with_stdio(false);
30 | int i, n, m;
31 | cin >> n >> m;
32 | vector> g(n);
33 | for(i = 0; i < m; i++) {
34 | int u, v; cin >> u >> v;
35 | g[--u].pb(--v);
36 | }
37 |
38 | int head = -1;
39 | vector vis(n), ans;
40 | ans.reserve(n);
41 | for(i = 0; i < n; i++)
42 | if(!vis[i])
43 | Y([&](auto dfs, int v) -> void {
44 | vis[v] = 1;
45 | for(auto& x: g[v]) if(head == -1) {
46 | if(!vis[x]) dfs(x);
47 | else if(vis[x] == 1)
48 | ans.pb(head = x);
49 | } vis[v] = 2;
50 | if(~head and head < n) ans.pb(v);
51 | if(v == head) head = n;
52 | })(i);
53 |
54 | reverse(all(ans));
55 | if(ans.empty())
56 | return cout << "IMPOSSIBLE" << '\n', 0;
57 |
58 | cout << sz(ans) << '\n';
59 | for(auto& x: ans)
60 | cout << x+1 << ' ';
61 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Round Trip.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 17.05.2021 23:01:22
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | template class Y {
16 | T f;
17 | public:
18 | template explicit Y(U&& f): f(forward(f)) {}
19 | template decltype(auto) operator()(Args&&... args) {
20 | return f(ref(*this), forward(args)...);
21 | }
22 | }; template Y(T) -> Y;
23 |
24 |
25 | const int64_t k_II = 2e18;
26 | const int INF = 2e9, MOD = 1e9+7;
27 | const int N = 2e5 + 5;
28 |
29 | int main() {
30 | cin.tie(nullptr)->sync_with_stdio(false);
31 | int i, n, m;
32 | cin >> n >> m;
33 | vector> g(n);
34 | for(i = 0; i < m; i++) {
35 | int u, v; cin >> u >> v; u--, v--;
36 | g[u].pb(v); g[v].pb(u);
37 | }
38 |
39 | int head = -1;
40 | vector vis(n), ans;
41 | ans.reserve(n);
42 |
43 | auto dfs = Y([&](auto dfs, int v, int p) -> void {
44 | vis[v] = true;
45 | for(auto& x: g[v]) if(x^p and head == -1) {
46 | if(vis[x] and ans.empty()) head = x;
47 | if(!vis[x]) dfs(x, v);
48 | }
49 | if(~head) {
50 | ans.pb(v);
51 | if(v == head)
52 | head = -1;
53 | }
54 | });
55 |
56 | for(i = 0; i < n; i++)
57 | if(!vis[i]) dfs(i, -1);
58 |
59 | if(ans.empty())
60 | return cout << "IMPOSSIBLE" << '\n', 0;
61 |
62 | cout << sz(ans)+1 << '\n';
63 | for(auto& x: ans)
64 | cout << x+1 << ' ';
65 | cout << ans.front()+1;
66 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Shortest Routes I.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 18.05.2021 00:21:28
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 | auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
15 | auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
16 |
17 | const int64_t k_II = 2e18;
18 | const int INF = 2e9, MOD = 1e9+7;
19 | const int N = 2e5 + 5;
20 |
21 | int main() {
22 | cin.tie(nullptr)->sync_with_stdio(false);
23 | int i, n, m;
24 | cin >> n >> m;
25 | vector>> g(n);
26 | for(i = 0; i < m; i++) {
27 | int u, v, w; cin >> u >> v >> w;
28 | g[--u].pb(--v, w);
29 | }
30 |
31 | struct state {
32 | int v; int64_t dist;
33 | state(int v, int64_t dist): v(v), dist(dist) {}
34 | bool operator<(const state& o) const {
35 | return dist > o.dist;
36 | }
37 | };
38 |
39 | vector d(n, k_II);
40 | priority_queue pq;
41 | pq.emplace(0, d[0] = 0);
42 | while(!pq.empty()) {
43 | state top = pq.top(); pq.pop();
44 | if(top.dist > d[top.v]) continue;
45 | for(auto& [to, w]: g[top.v])
46 | if(chmin(d[to], top.dist + w))
47 | pq.emplace(to, d[to]);
48 | }
49 |
50 | for(i = 0; i < n; i++)
51 | cout << d[i] << " \n"[i == n-1];
52 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Shortest Routes II.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 18.05.2021 00:28:26
4 | **/
5 | #ifdef W
6 | #include
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 | auto chmax = [](auto& A, auto&& B) { return B > A? A = B, true : false; };
15 | auto chmin = [](auto& A, auto&& B) { return B < A? A = B, true : false; };
16 |
17 | const int64_t k_II = 1e18;
18 | const int INF = 2e9, MOD = 1e9+7;
19 | const int N = 500 + 5;
20 | int64_t d[N][N];
21 |
22 | int main() {
23 | cin.tie(nullptr)->sync_with_stdio(false);
24 | int i, j, k, n, m, Q;
25 | cin >> n >> m >> Q;
26 | for(i = 0; i < n; i++)
27 | for(j = 0; j < n; j++)
28 | if(i ^ j) d[i][j] = k_II;
29 |
30 | for(i = 0; i < m; i++) {
31 | int u, v, w; cin >> u >> v >> w; --u; --v;
32 | chmin(d[u][v], w); chmin(d[v][u], w);
33 | }
34 |
35 | for(k = 0; k < n; k++)
36 | for(i = 0; i < n; i++)
37 | for(j = 0; j < n; j++)
38 | chmin(d[i][j], d[i][k] + d[k][j]);
39 |
40 | while(Q--) {
41 | int u, v; cin >> u >> v; --u; --v;
42 | cout << (d[u][v] == k_II? -1 : d[u][v]) << '\n';
43 | }
44 | } // ~W
--------------------------------------------------------------------------------
/CSES/Graph Algorithms/Teleporters Path.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 06.03.2021 19:01:10
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | template class Y {
16 | T f;
17 | public:
18 | template explicit Y(U&& f): f(forward(f)) {}
19 | template decltype(auto) operator()(Args&&... args) {
20 | return f(ref(*this), forward(args)...);
21 | }
22 | }; template Y(T) -> Y;
23 |
24 | struct edge_t {
25 | int u{-1}, v{-1}; int64_t w{0};
26 | edge_t() = default;
27 | edge_t(int u, int v, int64_t w = 0) : u(u), v(v), w(w) {}
28 | bool operator<(const edge_t& other) { return w < other.w; }
29 | friend ostream& operator<<(ostream& os, const edge_t& e) { os << e.u << ' ' << e.v << ' ' << e.w; return os; }
30 | };
31 |
32 | const int64_t DESPACITO = 2e18;
33 | const int INF = 2e9, MOD = 1e9+7;
34 | const int N = 2e5 + 5;
35 |
36 | int main() {
37 | cin.tie(nullptr)->sync_with_stdio(false);
38 | int i, n, m;
39 | cin >> n >> m;
40 | vector indeg(n), outdeg(n);
41 | vector> g(n);
42 | for(i = 0; i < m; i++) {
43 | int u, v; cin >> u >> v;
44 | outdeg[--u]++; indeg[--v]++;
45 | g[u].pb(v);
46 | }
47 |
48 | bool bad = false;
49 | for(i = 1; i < n-1; i++)
50 | bad |= outdeg[i] ^ indeg[i];
51 |
52 | bad |= outdeg[0]-indeg[0] ^ 1;
53 | bad |= indeg[n-1]-outdeg[n-1] ^ 1;
54 |
55 | if(bad)
56 | return cout << "IMPOSSIBLE" << '\n', 0;
57 |
58 | vector tour;
59 | tour.reserve(m+1);
60 | Y([&](auto dfs, int v) -> void {
61 | while(outdeg[v]-- > 0)
62 | dfs(g[v][outdeg[v]]);
63 | tour.pb(v);
64 | })(0);
65 |
66 | for(i = 0; i < n; i++)
67 | if(outdeg[i] > 0)
68 | return cout << "IMPOSSIBLE" << '\n', 0;
69 |
70 | reverse(all(tour));
71 | for(auto& x: tour) cout << ++x << ' ';
72 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Apartments.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 03.02.2021 00:51:35
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, j, n, m, k, ans = 0;
22 | cin >> n >> m >> k;
23 | vector a(n), b(m);
24 | for(auto& x: a) cin >> x;
25 | for(auto& x: b) cin >> x;
26 | sort(all(a)); sort(all(b));
27 |
28 | for(i = j = 0; i < n; i++) {
29 | while(j < m and b[j] < a[i]-k) j++;
30 | if(j < m and b[j] <= a[i]+k) ans++, j++;
31 | }
32 |
33 | cout << ans;
34 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Array Division.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 thew6rst
3 | 🍪 10.02.2021 16:17:31
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, n, k;
22 | cin >> n >> k;
23 | vector a(n);
24 | for(auto& x: a) cin >> x;
25 |
26 | auto check = [&a, k](int64_t x) {
27 | int p = 1; int64_t cur = 0;
28 | for(auto y: a) {
29 | if(cur + y > x)
30 | p++, cur = 0;
31 | cur += y;
32 | } return p <= k;
33 | };
34 |
35 | int64_t lo = *max_element(all(a)), hi = DESPACITO;
36 | while(lo <= hi) {
37 | int64_t mid = lo + hi >> 1;
38 | if(check(mid)) hi = mid - 1;
39 | else lo = mid + 1;
40 | } cout << lo;
41 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Collecting Numbers II.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 thew6rst
3 | 🍪 10.02.2021 11:59:45
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, n, m, ans = 0;
22 | cin >> n >> m;
23 | vector pos(n+2, INF), a(n+2);
24 | for(i = 1; i <= n; i++) {
25 | cin >> a[i];
26 | pos[a[i]] = i;
27 | }
28 |
29 | for(i = 1; i <= n; i++)
30 | ans += pos[i] < pos[i-1];
31 |
32 | auto upd = [&](int x, int delta) {
33 | ans += pos[x] < pos[x-1]? delta : 0;
34 | ans += pos[x+1] < pos[x]? delta : 0;
35 | };
36 |
37 | while(m--) {
38 | int x, y; cin >> x >> y;
39 | upd(a[x], -1); upd(a[y], -1);
40 | swap(a[x], a[y]); pos[a[x]] = x; pos[a[y]] = y;
41 | upd(a[x], +1); upd(a[y], +1);
42 | if(abs(a[x]-a[y]) == 1) {
43 | int p = min(a[x], a[y]), q = max(a[x], a[y]);
44 | ans += pos[p] < pos[q]? +1 : -1;
45 | }
46 | cout << ans << '\n';
47 | }
48 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Collecting Numbers.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 thew6rst
3 | 🍪 10.02.2021 11:59:45
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, n, ans = 1;
22 | cin >> n;
23 | vector pos(n);
24 | for(i = 0; i < n; i++) {
25 | int x; cin >> x;
26 | pos[--x] = i;
27 | }
28 |
29 | for(i = 1; i < n; i++)
30 | ans += pos[i] < pos[i-1];
31 |
32 | cout << ans;
33 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Concert Tickets.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 03.02.2021 01:26:32
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, n, m;
22 | cin >> n >> m;
23 | multiset s;
24 | for(i = 0; i < n; i++) {
25 | int x; cin >> x;
26 | s.insert(-x);
27 | }
28 |
29 | while(m--) {
30 | int x; cin >> x;
31 | auto it = s.lower_bound(-x);
32 | if(it == s.end())
33 | cout << -1 << '\n';
34 | else {
35 | cout << -*it << '\n';
36 | s.erase(it);
37 | }
38 | }
39 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Distinct Numbers.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 03.02.2021 01:31:05
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, n;
22 | cin >> n;
23 | vector a(n);
24 | for(auto& x: a) cin >> x;
25 | sort(all(a));
26 | a.erase(unique(all(a)), a.end());
27 | cout << sz(a);
28 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Factory Machines.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 thew6rst
3 | 🍪 11.02.2021 11:55:36
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 1.05e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, n; int64_t k;
22 | cin >> n >> k;
23 | vector a(n);
24 | for(auto& x: a) cin >> x;
25 |
26 | auto check = [&a, k](int64_t t) {
27 | int64_t res = 0;
28 | for(auto& x: a) {
29 | res += t / x;
30 | if(res >= k) return true;
31 | }
32 | return res >= k;
33 | };
34 |
35 | int64_t lo = 1, hi = DESPACITO;
36 | while(lo <= hi) {
37 | int64_t mid = lo + hi >> 1;
38 | if(check(mid)) hi = mid - 1;
39 | else lo = mid + 1;
40 | } cout << lo;
41 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Ferris Wheel.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 the_hyp0cr1t3
3 | 🍪 03.02.2021 00:56:58
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, j, n, x, ans = 0;
22 | cin >> n >> x;
23 | vector a(n);
24 | for(auto& x: a) cin >> x;
25 | sort(all(a));
26 | for(i = 0, j = n-1; i < j; j--) {
27 | if(a[i] + a[j] <= x) i++;
28 | ans++;
29 | } if(i == j) ans++;
30 |
31 | cout << ans;
32 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Josephus Problem I.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 thew6rst
3 | 🍪 10.02.2021 13:21:34
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 | #include
15 | #include
16 | using namespace __gnu_pbds;
17 | template>
18 | using ordered_set = tree;
19 | #define ook order_of_key // count of elements strictly smaller than k
20 | #define fbo find_by_order // iterator to kth element starting from 0
21 |
22 | const int64_t DESPACITO = 2e18;
23 | const int INF = 2e9, MOD = 1e9+7;
24 | const int N = 2e5 + 5;
25 |
26 | int main() {
27 | cin.tie(nullptr)->sync_with_stdio(false);
28 | int i, n, k = 1, pos = 0;
29 | cin >> n;
30 | ordered_set os;
31 | for(i = 1; i <= n; i++)
32 | os.insert(i);
33 | do {
34 | pos = (pos + k) % n;
35 | auto it = os.fbo(pos);
36 | cout << *it << " \n"[i == n];
37 | os.erase(it);
38 | } while(--n);
39 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Josephus Problem II.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 thew6rst
3 | 🍪 10.02.2021 13:21:34
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 | #include
15 | #include
16 | using namespace __gnu_pbds;
17 | template>
18 | using ordered_set = tree;
19 | #define ook order_of_key // count of elements strictly smaller than k
20 | #define fbo find_by_order // iterator to kth element starting from 0
21 |
22 | const int64_t DESPACITO = 2e18;
23 | const int INF = 2e9, MOD = 1e9+7;
24 | const int N = 2e5 + 5;
25 |
26 | int main() {
27 | cin.tie(nullptr)->sync_with_stdio(false);
28 | int i, n, k, pos = 0;
29 | cin >> n >> k;
30 | ordered_set os;
31 | for(i = 1; i <= n; i++)
32 | os.insert(i);
33 | do {
34 | pos = (pos + k) % n;
35 | auto it = os.fbo(pos);
36 | cout << *it << " \n"[i == n];
37 | os.erase(it);
38 | } while(--n);
39 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Maximum Subarray Sum II.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 thew6rst
3 | 🍪 11.02.2021 15:31:06
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 | int a[N];
19 | int64_t pref[N];
20 |
21 | int main() {
22 | cin.tie(nullptr)->sync_with_stdio(false);
23 | int i, n, x, y; int64_t ans = -DESPACITO;
24 | cin >> n >> x >> y;
25 | for(i = 1; i <= n; i++) {
26 | cin >> a[i];
27 | pref[i] = pref[i-1] + a[i];
28 | }
29 |
30 | multiset ms;
31 | for(i = 1; i <= n; i++) {
32 | if(i-x >= 0) ms.insert(pref[i-x]);
33 | if(!ms.empty()) ans = max(ans, pref[i] - *ms.begin());
34 | if(i-y >= 0) ms.erase(ms.find(pref[i-y]));
35 | }
36 |
37 | cout << ans;
38 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Maximum Subarray Sum.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 thew6rst
3 | 🍪 10.02.2021 11:41:10
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, n; int64_t cur = 0, ans = -DESPACITO;
22 | cin >> n;
23 | for(i = 0; i < n; i++) {
24 | int x; cin >> x;
25 | cur += x;
26 | ans = max(ans, cur);
27 | if(cur < 0) cur = 0;
28 | } cout << ans;
29 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Missing Coin Sum.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 thew6rst
3 | 🍪 10.02.2021 11:56:37
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, n; int64_t ans = 1;
22 | cin >> n;
23 | vector a(n);
24 | for(auto& x: a) cin >> x;
25 | sort(all(a));
26 | for(auto& x: a) {
27 | if(x > ans) break;
28 | ans += x;
29 | } cout << ans;
30 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Movie Festival II.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 thew6rst
3 | 🍪 10.02.2021 16:31:28
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, n, k, ans = 0;
22 | cin >> n >> k;
23 | vector> a(n);
24 | for(auto& [r, l]: a) cin >> l >> r;
25 | sort(all(a));
26 |
27 | multiset have;
28 | for(i = 0; i < k; i++)
29 | have.insert(0);
30 |
31 | for(auto& [r, l]: a) {
32 | auto it = have.upper_bound(l);
33 | if(it != have.begin()) {
34 | have.erase(--it);
35 | ans++; have.insert(r);
36 | }
37 | } cout << ans;
38 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Movie Festival.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 thew6rst
3 | 🍪 10.02.2021 11:29:35
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int n, ans = 0, cur = -1;
22 | cin >> n;
23 | vector> events(n);
24 | for(auto &[x, y]: events) cin >> y >> x;
25 | sort(all(events));
26 | for(auto &[x, y]: events)
27 | if(y >= cur) ans++, cur = x;
28 | cout << ans;
29 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Nearest Smaller Values.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 thew6rst
3 | 🍪 10.02.2021 15:48:39
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 |
15 | const int64_t DESPACITO = 2e18;
16 | const int INF = 2e9, MOD = 1e9+7;
17 | const int N = 2e5 + 5;
18 |
19 | int main() {
20 | cin.tie(nullptr)->sync_with_stdio(false);
21 | int i, n;
22 | cin >> n;
23 | vector a(n);
24 | stack st;
25 | for(i = 0; i < n; i++) {
26 | cin >> a[i];
27 | while(!st.empty() and a[st.top()] >= a[i])
28 | st.pop();
29 | cout << (st.empty()? 0 : st.top() + 1) << " \n"[i == n-1];
30 | st.push(i);
31 | }
32 | } // ~W
--------------------------------------------------------------------------------
/CSES/Sorting and Searching/Nested Ranges Check.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | 🍪 thew6rst
3 | 🍪 10.02.2021 13:41:04
4 | **/
5 | #ifdef W
6 | #include "k_II.h"
7 | #else
8 | #include
9 | using namespace std;
10 | #endif
11 | #define pb emplace_back
12 | #define all(x) x.begin(), x.end()
13 | #define sz(x) static_cast(x.size())
14 | #include