Skip to content

Latest commit

 

History

History
366 lines (352 loc) · 24.3 KB

README.md

File metadata and controls

366 lines (352 loc) · 24.3 KB

Feel free to follow my progress on my main online judge profiles:


Solutions index

· #brute-force · #greedy · #ad-hoc · #data-structures · #miscellaneous · #math · #divide-&-conquer · #strings · #geometry · #dynamic-programming · #graphs ·

graphs

  • maximum flow
    • 🙂 uva/820
    • 😳 uva/10779 → each friend receives 1 flow unit of a sticker and offers a flow of other sticker; Bob also offers, but he receives 1 flow unit of each sticker; maximized it
    • minimum cost
  • minimum spanning tree (MST)
    • 🙂 uva/1174
    • 🙂 uva/11710
    • 🙂 spoj/MST
    • 🙂 uva/10034
    • minimax path
      • 🙂 uva/10048
      • 😳 uva/10099: maximin path; find the minimum cost of the maximum path from s to t → apply kruskal to get the maximum spanning tree, but stop it when s and t connect
    • minimum spanning subgraph
      • 😳 uva/10397: given an implicit complete graph and some edges, compute the cost of the minimum spanning subgraph
  • shortest path
    • single-source
      • weighted graph
        • 😳 uva/10806: find the global shortest path from 0 to V-1 (round trip), without repeting edges
        • 🥵 live-archive/3652: dijkstra on a given implicit graph as a 2D grid
        • 😳 uva/12144: almost shortest path
        • 🥵 uva/11367 → find the shortest path on state-space graph, where each vertex represent a city and a level of car fuel
        • 😳 uva/11833 → build the graph already with the given constraints
      • negative-weighted and cycle graph
        • 😳 at-coder/ABC137-E: longest distance from 0 to V-1, checking for positive cycle that are part of that path (0 to V-1)
      • unweighted graph
        • 😳 uva/12797 → apply a BFS for each subset of letters possible (only 2^10) using bitmask
    • all-pairs
  • traversal
    • 🙂 uva/11831 → consider grid as an implicit graph and walk through it, or just rotate the robot, for each received instruction
    • 😳 uva/11906 → consider grid as an implicit graph and walk through it avoiding redundant positions (nr, nc)
    • 😳 code-jam/2020-1A-pascal-walk-TLE
    • topological sorting
    • edge classification
      • 🙂 codeforces/104-C: check if the given undirected graph can be represented as a set of three or more rooted trees, whose roots are connected by a simple cycle → check if the graph is connective and has only one cycle
      • 🙂 codeforces/510-B: check if a given implicit undirected graph has a cycle of length >= 4
    • strongly connected components (SCC)
      • 😳 uva/11504 → count the number of SCCs without incoming edge from a vertex of another SCC
    • bridges or articulation points
      • 🙂 uva/796
      • 🥵 codeforces/178-B3: given an undirected graph, count how many bridge edges exists between 2 query vertices → group the vertices connected by non-bridge edge during dfs; generate a tree considering each group as a vertice, and using only the bridge edges; find the distance between 2 query vertices of that tree with LCA in O(1)
      • 🥵 uva/12363: given an undirected graph, check if there is a unique path between 2 query vertices → there is a unique path between s and t if the path between them is formed only by bridge edges; for optimize, keep sets for the vertices that are on the same connected component in the pruned graph (with only bridge edges)
    • flood fill
      • 🥵 uva/1103 → consider each pixel as a vertex of an implicit graph, then identify each hieroglyph counting the number of white CCs within it
      • 🙂 uva/11094
    • depth-first search (DFS)
  • specials
    • 😳 uva/12442: given a graph with all vertices with out-degree 1, find the vertice that reaches the most vertices
    • tree
      • 🙂 codeforces/115-A: given a forest, find the length of the longest path
      • 🙂 spoj/LABYR1: compute the diameter of a given implicit tree
      • 😳 spoj/ONP: infix to postfix conversion → see that the given expression is the in-order traversal in a binary tree, then print post-order traversal recursively without building the tree
      • lowest common ancestor (LCA)
        • 🙂 timus/1471: given a weighted tree, find the distance between 2 query vertices
        • 😳 uva/12238: given a weighted tree, find the distance between 2 query vertices
    • bipartite
      • max cardinality bipartite matching
        • 😳 uva/10080: max number of preys that can hide safely from an predator attack → consider a bipartite graph with edges that connect a gopher (prey) to an reachable hole
        • 😳 uva/11262 → binary search on answer + MCBM
      • bipartite checking
    • directed acyclic graph (DAG)
      • 🙂 uva/988: counting paths from 0 to V-1

dynamic programming

  • 😳 codeforces/1061-C → use space saving + all divisors in O(sqrt(n)) to optimize
  • 😳 uri/2699: given a 1000-digit number S with some digits hidden, determine the minimum S possible such that S % N == 0
  • 🙂 uva/10003
  • 🙂 spoj/LINEUP → use bitmask
  • 🙂 uva/11450
  • 😳 uva/10721
  • 🙂 spoj/TRT → don't memoize the current day
  • 🙂 uva/10337
  • 🙂 spoj/RPLB
  • 🙂 uva/11000
  • 🙂 timus/1225
  • 🙂 uva/10943
  • 😳 codeforces/608-C
  • 😳 uva/10651 → use bitmask
  • 😳 codeforces/166-E
  • 🙂 uva/10911 → use bitmasks
  • LCS reduced to LIS
    • 😳 uva/12747: edit distance with only inserting and deleting between two permutations from 1 to N
    • 😳 uva/10635
  • 0-1 knapsack
  • coin change
    • 🙂 uva/11407 → consider the coins as the perfect squares
    • 😳 uva/166: coin change with limited amount of coins + greedy coin change with unlimited amount of coins; I don't know why, but it works without memo
    • 🙂 codeforces/189-A
  • max 2D range sum
    • 🥵 uva/10755: max 3D range sum → use max 2D range sum in two of the three dimensions and max 1D range sum (kadane) on the third dimension
    • 🙂 uva/108
  • longest common subsequence (LCS)
    • 🙂 spoj/IOIPALIN: given a string, determine the minimal quantity of characters to be inserted into it in order to obtain a palindrome → compute length of string minus lcs between string and reversed string
    • 🙂 spoj/ADASEQEN
  • longest palindromic subsequence (LPS)
  • traveling salesman problem (TSP)
  • subset sum
    • 🙂 uva/624: print the subset of A with the max sum k, k <= K
    • 😳 uva/11658: find the smallest sum s of a subset of A, s >= S → scan as %d.%d
    • 😳 uva/10616: given an array of size N, count how many subsets of size M have their sum divisible by D → use module arithmetic
    • 🙂 uri/1203: check if any subset of A has a sum equal to K
    • 😳 uri/2089 → optimize memory
    • with repetition
  • max 1D range sum
  • longest increasing subsequence (LIS)
    • 😳 uva/10131 → sort elephants based on increasing kg, then apply LDS on iq
    • 🥵 uva/481
    • 😳 uri/1517
    • 😳 uva/11456 → find the max(lis[i]+lds[i]-1) for all i in [0 .. N-1], being i where the subsequence starts
  • digit
    • 😳 uri/1707: given two numbers x and y, compute the sum of the decimal digits of the odd numbers in the range [min(x,y) .. max(x,y)]
    • 🙂 spoj/CPCRC1C

geometry

strings

  • suffix array
    • 😳 spoj/SARRAY: just build a suffix array in O(N * log N)

divide & conquer

  • 😳 codeforces/768-B → knowing the size of the final array with a little math, use the same idea to query a range in a segment tree

math

  • ad-hoc
  • matrix exponentiation
    • 🙂 uva/10229 → transform the fibonacci recurrence into a matrix exponentiation
  • game theory
    • minimax
      • 😳 uva/10111: given a state of a tic tac toe board, check if X will win independent of the O movement → minimax + memo + backtracking
      • 😳 uva/12484: alberto and wanderley take one of two cards at the edges of the cards sequence, alberto want maximize it → fill memo table in row-major order
      • optimized
        • 🥵 uva/847: multiplication game → note that if Stan always multiply by 9 and Ollie by 2, it's still an optimal solution
  • number theory
    • 😳 uri/2291: print n-th divine number, the one that is equal to the sum of the sum of each divisor from 1 to n → think like sieve
    • 😳 uva/547: find the longest DDF sequence → pre-process a array f, where f[i] is the sum of digits of all positive factors of i; think like sieve
    • greatest common divisor (GCD)
      • 🙂 codeforces/822-A
      • 😳 codeforces/75-C: find the gcd(a, b) that is in a query range [lo .. hi] → note that all divisors of gcd(a, b) are also divisors of a and b
      • 🙂 codeforces/854-A: given n, determine maximum possible proper (a < b) and irreducible fraction a/b such that a+b == n
    • module arithmetic
      • 🙂 uva/374: compute (a^n) % m, where a <= 2^31 and n <= 2^31 → use fast power with mod
      • 🙂 uva/10176: is a given binary number of 100 digits divisible by 131071?
    • prime numbers
      • 🙂 spoj/PRIONPRI: prime checking
      • prime factorization
        • 🥵 uri/2661: compute the number of divisors of n that can be written as the product of two or more distinct prime numbers (without repetition), 1 <= n <= 10^12 → note that the product of any combination of prime factors of a number will always be a divisor of that number
        • 🙂 uva/10042
        • 😳 uva/10139: do n! is divisible by m? → check if the prime factors of m are contained in the prime factors of n!
      • sieve of eratosthenes
        • 😳 codeforces/576-A
        • 🙂 codeforces/230-B: check if a large n has exactily three distinct positive divisors → check if sqrt(n) is prime
        • 😳 uva/10539: compute the quantity of non-prime numbers in [lo .. hi] which are divisible by only a single prime number, 0 < lo <= hi < 10^12 → generate all powers of each prime number
        • 🙂 spoj/AMR11E: print the n-th number that has at least 3 distinct prime factors → use modified sieve
        • 😳 spoj/PRIME1: print all primes p in [m .. n], 0 <= m <= n <= 10^9, n-m <= 10^5 → sieve + prime checking
  • combinatorics
    • 🙂 codeforces/459-B: find the max difference between numbers of a given array and the number of ways to get it
    • 🙂 codeforces/131-B
    • combinations
      • multi-combinations
      • binomial coefficient
        • 🥵 uri/2972: calculate the sum of C(N, k)%2 for all k in [0 .. n], i.e., how many odd combinations of k heads between n coins there are → just compute 2^qtyBitsOn(n)
        • 🙂 codeforces/844-B

miscellaneous

data structures

  • sparse table
  • union-find disjoint sets (UFDS)
  • segment tree
    • 🙂 codeforces/339-D
    • 🙂 live-archive/6139: range product query
    • lazy propagation
      • 😳 uva/11402 → build a segment tree for RSQ, but keep in lazy[v] the type of pending operation to be performed in that interval of A
      • 😳 uri/2658 → build a segment tree for RSQ, but store an array of size 9 in tree[v], where tree[v][n] indicates the frequency that the note n appears in that interval

ad-hoc

greedy

brute force

  • recursive backtracking
    • 😳 uva/10503: given domino pieces, check if it is possible to arrive at a target piece from an initial piece using N intermediate pieces (possibly rotating them) → DFS + backtracking
    • 🙂 spoj/MKJUMPS: given an unweighted implicit graph, count the longest possible path → DFS + backtracking
    • 🙂 code-jam/2020-QR-indicium-TLE: generate M, where M[i][j] is any value in [1 .. N] (2 <= N <= 5), but without repeting a value in same line or column, and with the sum of the main diagonal equal to K
    • 🙂 uva/10344: check if some arithmetic expression of 5 given numbers will result in 23 → check all combination of operators for each permutation of the given numbers
    • 🙂 live-archive/2883: chess with a single horse against up to 8 pawns; print the length of the minimum sequence of horse moves if it wins
    • pruned permutations
    • n-queens
    • sudoku
  • iterative