Skip to content

brpapa/algorithms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

The notebook folder contains general-purpose algorithms, and the solutions folder contains the solved problems. Feel free to follow my progress on my main online judge profiles:


Solutions categorized into themes

Β· #divide-&-conquer Β· #brute-force Β· #greedy Β· #ad-hoc Β· #graphs Β· #geometry Β· #dynamic-programming Β· #math Β· #searching Β· #miscellaneous Β·

miscellaneous

searching

  • segment tree
    • πŸ™‚ codeforces/339-D
    • lazy propagation
      • 😳 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
      • 😳 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
  • binary search
    • 😳 codeforces/1324-D: given the A and B arrays, count the quantity of pairs i < j such that A[i]+A[j] > B[i]+B[j] β†’ diff being the A-B array, count, for all i in [0 .. N-2], how many times -diff[i] < diff[j], for all j in [i+1 .. N-1]
    • 😳 codeforces/1284-B
    • 😳 codeforces/91-B: given the array a, compute the array d, where d[i] = j-(i+1) for the max j such that a[j] < a[i] β†’ apply binary search on preprocessed array mn, filled from right to left
    • πŸ™‚ spoj/PAIRS1: given the A array, count the quantity of pairs i < j such that A[i]-A[j] == K β†’ search on the sorted array A by the A[n]+K, for all n in [0 .. N-1]
    • on answer

math

  • combinatorics
    • πŸ™‚ codeforces/131-B
    • πŸ™‚ codeforces/459-B: find the max difference between numbers of a given array and the number of ways to get it
    • fibonacci numbers
      • 😳 uva/10334 β†’ compute (n+2)-th fibonnaci number
    • combinations
      • binomial coefficient
        • πŸ™‚ codeforces/844-B
        • πŸ₯΅ 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)
      • multi-combinations
  • ad-hoc
  • 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
    • prime numbers
      • πŸ™‚ spoj/PRIONPRI: prime checking
      • sieve of eratosthenes
        • πŸ™‚ spoj/AMR11E: print the n-th number that has at least 3 distinct prime factors β†’ use modified sieve
        • πŸ™‚ codeforces/230-B: check if a large n has exactily three distinct positive divisors β†’ check if sqrt(n) is prime
        • 😳 codeforces/576-A
        • 😳 spoj/PRIME1: print all primes p in [m .. n], 0 <= m <= n <= 10^9, n-m <= 10^5 β†’ sieve + prime checking
        • 😳 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
      • 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/10139: do n! is divisible by m? β†’ check if the prime factors of m are contained in the prime factors of n!
        • πŸ™‚ uva/10042
    • greatest common divisor (GCD)
      • 😳 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/822-A
      • πŸ™‚ 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?
  • 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

dynamic programming

  • πŸ™‚ uva/10943
  • 😳 uva/10721
  • πŸ™‚ uva/10337
  • πŸ™‚ uva/10003
  • 😳 codeforces/166-E
  • πŸ™‚ spoj/TRT β†’ don't memoize the current day
  • πŸ™‚ uva/11450
  • 😳 codeforces/1061-C β†’ use space saving + all divisors in O(sqrt(n)) to optimize
  • 😳 uva/10651 β†’ use bitmasks
  • 0-1 knapsack
  • 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
  • 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
  • coin change
    • 😳 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
    • πŸ™‚ uva/11407 β†’ consider the coins as the perfect squares
    • πŸ™‚ codeforces/189-A
  • subset sum
    • πŸ™‚ uri/1203: check if any subset of A has a sum equal to K
    • 😳 uva/10616: given an array of size N, count how many subsets of size M have their sum divisible by D β†’ use module arithmetic
    • 😳 uva/11658: find the smallest sum s of a subset of A, s >= S β†’ scan as %d.%d
    • πŸ™‚ uva/624: print the subset of A with the max sum k, k <= K
    • with repetition
  • traveling salesman problem (TSP)
  • longest increasing subsequence (LIS)
    • 😳 uri/1517
    • 😳 uva/10131 β†’ sort elephants based on increasing kg, then apply LDS on iq
    • 😳 uva/11456 β†’ find the max(lis[i]+lds[i]-1) for all i in [0 .. N-1], being i where the subsequence starts
  • max 1D range sum
  • 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

geometry

graphs

  • 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
    • bipartite checking
    • bridges or articulation points
      • πŸ₯΅ 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)
      • πŸ™‚ uva/796
    • 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
    • topological sorting
    • strongly connected components (SCC)
      • 😳 uva/11504 β†’ count the number of SCCs without incoming edge from a vertex of another SCC
    • depth-first search (DFS)
    • edge classification
      • πŸ™‚ codeforces/510-B: check if a given implicit undirected graph has a cycle of length >= 4
      • πŸ™‚ 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
  • specials
    • 😳 uva/12442: given a graph with all vertices with out-degree 1, find the vertice that reaches the most vertices
    • directed acyclic graph (DAG)
      • πŸ™‚ uva/988: counting paths from 0 to V-1
    • tree
      • πŸ™‚ spoj/LABYR1: compute the diameter of a given implicit tree
      • πŸ™‚ codeforces/115-A: given a forest, find the length of the longest path
      • 😳 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
  • shortest path
    • all-pairs
    • single-source
      • unweighted graph
        • 😳 uva/12797 β†’ apply a BFS for each subset of letters possible (only 2^10) using bitmask
      • weighted graph
        • 😳 uva/11833 β†’ build the graph already with the given constraints
        • πŸ₯΅ live-archive/3652: dijkstra on a given implicit graph as a 2D grid
        • πŸ₯΅ uva/11367 β†’ find the shortest path on state-space graph, where each vertex represent a city and a level of car fuel
        • 😳 uva/10806: find the global shortest path from 0 to V-1 (round trip), without repeting edges
      • 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)
  • minimum spanning tree (MST)
    • πŸ™‚ uva/1174
    • πŸ™‚ spoj/MST
    • πŸ™‚ uva/10034
    • πŸ™‚ uva/11710
    • minimum spanning subgraph
      • 😳 uva/10397: given an implicit complete graph and some edges, compute the cost of the minimum spanning subgraph

ad-hoc

greedy

brute force

  • iterative
    • πŸ™‚ uva/12801
    • πŸ™‚ uva/10360
    • πŸ™‚ codeforces/633-A: check if a diophantine equation has positive solution
    • πŸ™‚ uva/628
    • πŸ™‚ uva/725
    • 😳 uva/12792: simulation β†’ note that if you 'watch' a unique card, the full deck will become sorted as soon as this card reaches its original position
    • all permutations
    • all subsets
  • recursive backtracking
    • πŸ™‚ 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
    • 😳 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
    • πŸ™‚ 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
    • πŸ™‚ 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
    • sudoku
    • pruned permutations
    • n-queens

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

About

🎈My notebook and solutions for 300+ problems that I solved during practice for ACM-ICPC

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published