Giter Club home page Giter Club logo

cphb's Introduction

Competitive Programmer's Handbook

Competitive Programmer's Handbook is a modern introduction to competitive programming. The book discusses programming tricks and algorithm design techniques relevant in competitive programming.

CSES Problem Set

The CSES Problem Set contains a collection of competitive programming problems. You can practice the techniques presented in the book by solving the problems.

https://cses.fi/problemset/

License

The license of the book is Creative Commons BY-NC-SA 4.0.

Other books

Guide to Competitive Programming is a printed book, published by Springer, based on Competitive Programmer's Handbook. There is also a Russian edition Олимпиадное программирование (Olympiad Programming) and a Korean edition 알고리즘 트레이닝: 프로그래밍 대회 입문 가이드.

https://cses.fi/book/

cphb's People

Contributors

julienc avatar l1nkus avatar ollpu avatar pllk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cphb's Issues

Union-find heuristics

There are also union-by-rank and path-compression heuristics. Should they be mentioned? Also the implementation could be shorter.

Filenames still in Finnish

johdanto.tex, kirj.tex, kkkk.pdf, kkkk.tex and luku**.tex are all still named in Finnish.

Hint: Git recognizes filename changes and preserves their history.

Z-algorithm implementation

The implementation for Z-algorithm is rather interesting since I believe the so called textbook implementation involves a lot of branches and potential one-off errors. Here's a rather short implementation that could be included in the book, similar to Manacher's algorithm. The key observation is that the while-loop will fail on the first iteration whenever we are strictly inside a longer match.

Any ideas on how to make it even simpler?

int l = 0, r = 0;
for(int i = 1; i < s.size(); i++) {
     // if i inside current rightmost match (l..r), 
     //   set z[i] to match "shadow" position (i-l) but capped at r-i+1
    if(i <= r) z[i] = min(r - i + 1, z[i - l]);

    // increase z[i] until mismatch or end
    while(z[i] + i < s.size() && s[z[i]] == s[i + z[i]]) z[i]++; 

    // update current rightmost match
    if(i + z[i] - 1 > r) {
         l = i;
         r = i + z[i] - 1;
     }
}

Implementation and some test cases on ideone.com in case you want to fork and try improving this one.

Page 4

g++ -std=c++11 -O2 -Wall code.cpp -o code

Why would I name the program as "code"?

Links to problems

Many people have asked links to practice problems, thus something should be done. Maybe an external list and not in the book?

Memset for distance arrays

Calling memset is more efficient and quicker to write, than a loop, to set all values to 0/INF.

What do you think about changing the loops in graph algorithms to memset?

Page 21

The time complexity table is misleading. O(n log n) is always ok when n = 10^6. Also O(n^2) is ok for n=5000, O(n^3) is ok for n=500.

Page 6

Maybe add "about" before -10^38...10^38

Page 5

Maybe there should be some warning for the use of getline. If you use cin before getline you should eat the whitespaces with cin>>ws; before the getline command.

Page 112

"If the graph is directed, the adjacency matrix representation can be extended so that the matrix contains the weight of the edge if the edge exists": i suppose there must be "weighted" unstead of "directed".

Page 9

Macro for make_pair is useless in c++11 and it is therefore a bad example. For example make_tuple could be more fitting for c++11.

Page 98

signed -> unsigned (in two places) in the first paragraph.

BFS Algorithm(Page 118)

z[s] = 1; e[x] = 0; q.push(x); while (!q.empty()) { int s = q.front(); q.pop(); // process node s for (auto u : v[s]) { if (z[u]) continue; z[u] = 1; e[u] = e[s]+1; q.push(u); } }

The BFS code on page 118 uses wrong variable -
z[s] = 1; should be changed to z[x] = 1;

Unisigned and 2^n-1

In this line

in an unsigned representation, the next number after $2^{n-1}$ is $0$.

$2^{n-1}$ should be $2^n-1$.

Similar to what is stated few lines above

An unsigned variable of $n$ bits can contain any integer between $0$ and $2^n-1$.

Page 22

"The time complexity of the algorithm is O(n^3), because it consists of three nested loops and each loop contains O(n) steps." I think that the reasoning "it consists of three nested loops and each loop contains O(n) steps" should lead to a O(n^4) algorithm.

27.2 example problem

This problem is a bad example, because there is an easier O(n log n) algorithm.

Negative distances in Dijkstra

Maybe it's not a good idea to use negative distances in the Dijkstra implementation. One could just use a proper priority queue.

Question on 27.1 Batch Processing: how to calculate minimum distance to a black square for all white squares in one BFS?

Hi @pllk, I really appreciate your huge efforts for writing this book and making it free and available. Thank you so much!

I have a question when I am reading 27.1 Batch Processing. The problem statement (not the latest version) is

Given a grid of size k by k initially consists of white squares and our task is to perform n operations, each of which is one of the following:

  • paint square (y,x) black

  • find the nearest black square to square (y,x) where the distance between
    squares (y1,x1) and (y2,x2) is |y1 - y2| + |x1 - x2|

There is a pre-processing step in each batch, to calculate for each square of the grid the smallest distance to a black square. This can be done in O(k^2) time using breadth-first search.

My question is: how is calculating for each square of the grid the smallest distance to a black square done in one BFS? (I can't figure out which square to start the BFS.)

Thanks in advance!

Btw, which is the better place, codeforces or this repo, to post questions when reading the book?

Page 97: missing '-'

On the first page of Chapter 10, the range of int is stated as being from 2^{n-1} to 2^{n-1}-1. The same range is mentioned twice, and both of them are missing '-' from the lower bound.

RMQ using sparse tables

The name "sparse table" should be mentioned. Also the description is not so good at the moment.

Reference for knight's tour

Parberry, Ian: An efficient algorithm for the Knight's tour problem. Discrete appl math. 73 (1997), 251--260

Page 15

"and unlike in the IOI, the students work together; there is even only one computer available for each team" doesn't sound right for me. Also the number of finals slots varies from year to year.

Square roots (page 20)

"A special property of square roots is that sqrt(n) = n/sqrt(n), so the square root sqrt(n) lies, in some sense, in the middle of the input."

This is difficult to understand.

13.2 Dijkstra implementation

This is not the usual implementation found in textbooks, maybe there should be more discussion why the implementation works.

Time complexity in the 'meet in the middle' example

I'm translating your book in Italian and I have a doubt. At the end of chapter 5, you state that "it is possible to check in $O(2^{n/2})$ time if the sum $x$ can be created from $S_A$ and $S_B$". Maybe I'm wrong, but I think that this could be possibile only if both $S_A and $S_B are sorted, as it seems in the example. If I'm wrong, could you explain more about how to achieve that complexity? Otherwise, could you add the assumption about sorting?

Dijsktra algorithm - priority queue

In your dijkstra algorithm you suggest using negative distances, as a workaround for priority_queue sorting by the maximum element by default.

I think, a better solution would be to use std::greater as a template argument.

priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> q;

Birthday paradox

Shouldn't this line be using term large instead of small?

The phenomenon in scenario 3 is known as the birthday paradox: if there are n people in a room, the probability that some two people have the same birthday is large even if n is quite small.

No ePub or Mobi Available.

Not a big deal, but I figured I would document this. For smaller devices (phones, smaller tablets) an eBook is preferable to a PDF.

Nim analysis

The analysis of nim and examples could be improved

Page 29

Maybe you should just say that the time and space complexities of counting sort are O(n+c) instead of writing about "a small constant".

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.