"Tricks" I learned while practicing

Revision en5, by rising_sea, 2020-10-30 03:17:59

Disclaimer: I personally don't find learning tricks to be all that interesting, not responsible for any rating drops LOLOLOL, also I'm a bit lazy so there are prob mistakes in here

Translate (AKA immediate implementation problem)

  1. The problem is obvious that it should just be some data structures e.g. Segment tree 474F - Ant colony
  2. Just DP, e.g. 489F - Special Matrices

Converting from brute force

  1. Sometimes it just works ??? this works especially well if you notice the input is extremely small
  2. Meet in the middle to be square root smaller 525E - Anya and Cubes, 1006F - Xor-Paths
  3. Backtracking to prune states
  4. Memoization (this is just dp lol)

Look at something in a new way

  1. Remove what seems to be impossible, NO it’s not cause you don’t know some fancy technique, just try you best to convert the impossibility into something familiar!!
  2. ROOT THE TREE, probably a third of random tree problems on cf can be solved with this, it’s pretty powerful, either can be direction recursion, or sometimes you need dp to store things 485D - Maximum Value (A bit different in that need to consider each node as a root)
  3. Sorting things 732F - Tourist Reform, 474F - Ant colony
  4. JUST look at the array from left to right and consider elements one by one~ this works really well with problems with subsequences in arrays because subsequences look hard but it becomes trivial when you actually just consider elements one by one Similarly, sometimes problems delete characters of a string/elements of array -> don’t think about how the string/array gets smaller, just think in terms of the original string/array
  5. Convert array to frequency array
  6. Splitting bigger things to smaller things/combining smaller things to bigger things

Splitting input into interesting groups

  1. Array problems -> fix one endpoint, interesting if we observe special properties as we extend right endpoint 689D - Friends and Subsequences
  2. Group input into two buckets, where different methods apply for each bucket, AKA sqrt algorithms (27.1 in cses), 1207F - Remainder Problem, 797E - Array Queries
  3. DP is a form of grouping?? many times it's hard cuz you can't find the important one 489F - Special Matrices
  4. Rectangle -> consider length/width as independent things 1284D - New Year and Conference
  5. Scanline -> intervals between event endpoints are interesting 1284D - New Year and Conference
  6. When you need to compare things one-to-one correspondence -> Canonical representations
  7. Order/smallest iteration that something repeats
  8. Complementary counting
  9. fix on something special to the problem and loop over/consider in terms of that
  10. Freely do things until the last step, but the last step automatically gets taken cared of anyways
  11. General independency, e.g. Minimize Manhatten distance = minimize on Xs + minimize on Ys
  12. = n is equivalent to (<= n) — (<= n-1) and vice versa

Find sparse/discrete/distinctive elements (AKA we like BIGGGGGG things)

  1. Binary exponentiation (when lots of identical things) 702E - Analysis of Pathes in Functional Graph, 621E - Wet Shark and Blocks
  2. There aren’t that many powers, powers are op 732E - Sockets
  3. Using a number’s binary representation
  4. Find representatives A character is sometimes enough to represent a whole string Winner of elimination games Endpoints of intervals are usually interesting
  5. Not that many distinct prime factors for any reasonable number

Summarizing info

  1. Preprocessing is op (range sums, segment tree) Prefix sum (l, r) = (1, r)-(1,l-1) is just too op
  2. DP space saving 489F - Special Matrices (Don't actually need to keep track of which row we are currently on)
  3. Keep track of cumulative result as we scan for dp
  4. Where you store several cum arrays/fenwick trees, each for a certain remainder x modulo m

Observe interesting things

  1. "Intermediate value theorem" 689D - Friends and Subsequences
  2. One choice affects another, intertwined => dp? flow?
  3. Move by one consideration (THIS IS SUPER POWERFUL && NONOBVIOUS, SOMETIMES THIS KILLS PROBLEM IMMEDIATELY)
  4. Symmetry
  5. Things don't matter doesn't matter how many times you turn sth on/off, it only depends on parity
  6. The process must stop at some point, it might seem you like you need to simulate to infinity, but actually you only need to do a finite number of times)
  7. Increasing sequence??
  8. Trick with "optimized can always look a certain way", this is when you go, let's say you have something that looks a certain way, then if you change it in some ways then it's even better -> oh so the optimized thing must look a certain way
  9. Simulating something once is easy, but tle when trying all inputs => binary search (especially if the problem asks for min/max)

How can we save?

  1. Use set/map maybe?: 1284D - New Year and Conference, 525E - Anya and Cubes
  2. Amortized analysis 732E - Sockets
  3. Sieve of Eratosthenes for analyzing complexity

DFS

  1. Standard counting
  2. Graph coloring
  3. For 2d grid
  4. Articulation points and bridges
  5. SCC
  6. Non obvious need to root the tree

Dijkstra

  1. Standard
  2. Fancier (keep track of something on top of distance)

other graphs

  1. Construct another graph based on original
  2. Can you go from source to destination with certain constraints
  3. Consider connected components separately
  4. Consider cycles
  5. Connected tree -> spanning tree
  6. Use dsu to union a component and treat is as one vertex

Sortings, relative order

  1. Reverse index/what you are thinking about
  2. Permutation inversions (ai > aj); useful to consider one element at a time
  3. Permutation/sortings with pairs containing original array index
  4. Somtimes sorting in an order helps solve the problem immediately (mentioned above)
  5. Indices/subindices/subsub indices -> practice to get familiar with this, it's just complicated to understand, but once you understand the problem is easy
  6. Using values as index
  7. idx/j subroutine, e.g. get counts of each block in 1, 1, 2, 2, 2, 3, 3, 3, 2, 2, 1, 1, 1, 1
  8. Use segment tree to store answer (forgot how exactly this works)

Local (greedy)

  1. Lexicographical order — ALWAYS consider digit by digit
  2. Powers of 2 consideration
  3. Again, move by one consideration
  4. Greedy where working out 1 or 2 things makes the rest work out

Global

  1. Can sometimes turn quadratics into linear by aggregating
  2. Double counting

Constructions

  1. Yes/No question? Sometimes one of yes or no direction is trivial
  2. Similarly, Even/odd consideration (sometimes the answer is always no for one of them)
  3. Think in simple global structure, aka don't think the problem is too hard, there needs to be something that is easy to think about
  4. Try to get rid of the condition in one go

Game theory

  1. Typical DP
  2. Some problems avoid the typical game theory dp thing Usually a big jump, where one player can just do something big and that's pretty much the end of game
  3. Elimination games => consider winner

Number theory

  1. Standard problem if you actually know all the basics
  2. Problems almost solved after considering gcd/consider when things are relatively prime (get extra assumption if divide by gcd)

Misc

  1. Algebra pattern finding, AKA simple induction; be careful with base case
  2. PIE
  3. WLOG one side is bigger
  4. TRIE for non obvious xor problem, tho this is a bit overused so hopefully you won't see it ever again

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en5 English rising_sea 2020-10-30 03:17:59 54
en4 English rising_sea 2020-10-29 21:40:30 928 Tiny change: 'm:489F] \n- Segment tree [problem:474F] \n\n###' -> 'm:489F] \n\n###' (published)
en3 English rising_sea 2020-10-29 21:28:32 3076
en2 English rising_sea 2020-10-29 21:20:36 3909
en1 English rising_sea 2020-10-29 21:11:42 509 Initial revision (saved to drafts)